您的位置:首页 > 数据库 > Mongodb

MongoDB一些进阶操作(索引,批量操作,分片)

2016-09-18 14:15 691 查看

http://blog.csdn.net/u014595019/article/details/50918664


建立索引

这篇文章讲了Mongodb中的一些常用索引操作,包括基础索引,文档索引,组合索引,唯一索引,删除索引等。摘要如下
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立基础索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ensureIndex</span>({age:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>})
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 后台建立基础索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.t</span>3<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ensureIndex</span>({age:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>} , {backgroud:true})
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立文档索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.factories</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ensureIndex</span>( { addr : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> } )<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立组合索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.factories</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ensureIndex</span>( { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"addr.city"</span> : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"addr.state"</span> : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> } )<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立唯一索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.t</span>4<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ensureIndex</span>({firstname: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, lastname: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>}, {unique: true})<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 删除索引</span>
db<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.t</span>4<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dropIndex</span>({firstname: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>})</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>


这些操作在pymongo中,与直接在数据库中的操作有些不同。
<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立非唯一索引</span>
db.collection.create_index([(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>,pymongo.ASCENDING)])
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立唯一索引</span>
db.collection.create_index([(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>,pymongo.ASCENDING)],unique=True)
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 建立文档索引</span>
db.collection.create_index([(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'field'</span>,pymongo.TEXT)]) #对名为field的项建立文档索引
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># 删除索引</span>
db.collection.drop_index([(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>,pymongo.ASCENDING)])</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>


前者建立非唯一索引。后者建立唯一索引。 

存在唯一索引时,每条信息的索引项必须唯一,否则在插入时会报错。若已经存在重复项,则在建立索引时报错。


批量操作

pymongo是具有批量操作的功能的。具体的api可以参考这里

如果是要执行插入指令,也可以选择db.collection.insert_many指令,这样要更方便一些。

但是,如果是要执行批量修改命令,就需要用到bulk_write功能了。 

通过pymongo中的bulk_write功能,可以实现在批量实现查询,插入,更新等功能。 

bulk_write的使用说明可以参考这里,需要搭配InsertOne,DeleteOne等一起使用。 

大概的使用例子如下
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">from pymongo import UpdateOne,UpdateMany
from pymongo import InsertOne,InsertMany
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 关于UpdateOne和UpdateMany的区别就是,如果查找到多个符合条件的值,</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># UpdateOne一次只会更新一条消息,而UpdateMany会一次更新所有符合要求的条目。</span>
requests=[
UpdateOne({<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'value1'</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'$set'</span>:data1}),
UpdateOne({<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'value2'</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'$set'</span>:data2}),
UpdateOne({<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'key'</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'value3'</span>},{<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'$set'</span>:data3}),
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
]
db.collection.bulk_write(requests)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>


UpdateOne的用法与db.collection.update用法比较类似,也有upsert等选项。


分片

先把参考网站放上 
http://kb.cnblogs.com/page/152995/ 

有空再补
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: