MongoDB权威指南(2)- 新增、修改、删除操作
jopen
13年前
<div id="article_content" class="article_content"> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>1.插入和保存document</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如前所述,向collection插入document使用insert方法</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">bar</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">baz</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果document里边没有"_id"键,"_id"会被自动创建</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">批量插入</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">批量插入是一种更高效的方法,传递给database一个document的数组,可以一次插入多个document。单个插入的时候,向 database传送一个document,前边会附加一个头部,告诉database在某个collection执行一次插入操作。批量插入只产生一个 TCP请求,意味着不用处理很多请求,同时也省掉了处理头部的时间。批量插入只能插入到一个collection里边去。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">批量插入只能用于应用程序接口,shell不支持(至少到目前还不支持)。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">另外,如果想导入数据(比如说从mysql),不要使用批量插入,使用命令行工具如mongoimport。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>2.删除document</strong></p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.remove()</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个命令会删除users里边的所有document。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">remove函数可以有一个查询用document做参数,以删除符合条件的document。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.mailing.list.remove({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">opt-out</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个命令删除所有"opt-out"为true的document。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">删除文档通常是一个非常快的操作,如果想清除整个collection,还有一种更快的方法,使用drop函数然后重建索引。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>3.更新document</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">udpate方法可以携带两个参数:</p> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>查询用document, 用于定位哪些document将会被更新</li> <li>修饰符document,用于描述如何修改找到的document</li> </ul> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">更新是原子性操作,先到达服务器的将会被先执行,后到达的会被后执行,所以,后边的会覆盖前边的修改。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">document替换</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">使用一个新的document来替换匹配的,上一篇文章里用的其实就是document替换,如</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}, joe);</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">document替换时一个常见的错误是当有多个匹配的document时候可能会导致duplicate key错误。举个例子,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">假设我们有好几个名字都叫joe的document,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.people.find()<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2b9f67a1f631733d917a7b</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">65</span> <span style="line-height:1.5;">},<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2b9f67a1f631733d917a7c</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">20</span> <span style="line-height:1.5;">},<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2b9f67a1f631733d917a7d</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">49</span> <span style="line-height:1.5;">},</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">现在,2号joe(20岁那个)生日到了,我们要给他的年龄加1,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> joe </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.people.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">20</span> <span style="line-height:1.5;">});<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2b9f67a1f631733d917a7c</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">20</span> <span style="line-height:1.5;"><br /> }<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> joe.age</span> <span style="line-height:1.5;">++</span> <span style="line-height:1.5;">;<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.people.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}, joe);<br /> E11001 duplicate key on update</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">Oh,出错了,怎么回事?数据库查找name为joe的document,找到的第一个是65岁那个,然后试图替换这个document,然而</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">数据库里边已经有一个"_id"为<span style="line-height:19px;">"</span><span style="line-height:19px;">4b2b9f67a1f631733d917a7c</span><span style="line-height:19px;">"</span><span style="line-height:19px;"> </span>的记录了,”_id"是不可重复的,所以就有个这个错误。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">所以执行document替换的时候要小心,确认你要替换的是唯一一个符合条件的。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">使用修饰符</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">通常情况下我们只想更新document的一部分,我们可以使用更新修饰符来做到这一点。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">假设我们有一个记录网站访问信息的一个collection,里边的document像这个样子</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">{<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b253b067525f35f94b60a31</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">url</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">www.example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">pageviews</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">52</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">pageviews是站点的访问次数,那么我想给它增加1的时候就可以这样子做</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.analytics.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">url</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">www.example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$inc</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">pageviews</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">"$inc"就是个更新修饰符,使用更新修饰符的时候,不能更新"_id"键的值。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">下边我们看看常用的更新修饰符</p> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>$set<br /> $set修饰符设定指定的key的值,如果key不存在就创建一个<br /> 假设我们有下边一个用户档案<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b253b067525f35f94b60a31</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">30</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">sex</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">male</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">location</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Wisconsin</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> 现在我们想给用户加一个项目,他喜爱的书籍,我们就可以用$set修饰符来做<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b253b067525f35f94b60a31</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">favorite book</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">war and peace</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> 看看结果<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b253b067525f35f94b60a31</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">30</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">sex</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">male</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">location</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Wisconsin</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">favorite book</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">war and peace</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> 如果用户不喜欢这本书了,喜欢另为一本,我们可以再次用$set来修改<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">favorite book</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">green eggs and ham</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> $set不仅可以改变值,还可以改变数据类型,如果用户喜欢的不是一本,而是很多书<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">favorite book</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> :<br /> ... [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">cat's cradle</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">foundation trilogy</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">ender's game</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]}})</span> </div> </div> 如果用户现在又觉得,他其实并不喜欢读书,我们可以用$unset来删除key<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$unset</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">favorite book</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}})<br /> </span> </div> </div> </li> <li><span style="line-height:19px;">$inc<br /> $inc修饰符只能用于数字,增加一个指定的数量<br /> <br /> </span></li> <li><span style="line-height:19px;">数组修饰符$push<br /> $push向数组尾部追加一个元素,如果数组不存在则创建一个数组<br /> 例如,我想给一篇博客文章添加评论,而评论这个key还不存在<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">title</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">A blog post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">...</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">title</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">A blog post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}, {$push : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> :<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">nice post.</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}}})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">title</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">A blog post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">...</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">nice post.</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }<br /> ]<br /> }</span> </div> </div> <span style="line-height:19px;">如果想添加另外一篇评论,就再次使用$push<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">title</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">A blog post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}, {$push : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> :<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">bob</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">bob@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">good post.</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}}})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">title</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">A blog post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">...</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">nice post.</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> },<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">bob</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">bob@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">good post.</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }<br /> ]<br /> }</span> </div> </div> <span style="line-height:19px;">如果想向数组里追加一个数组里没有的元素,我们可以在查询document里使用$ne(not equal,第四章查询里会有这种逻辑修饰符的说明),如:<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.papers.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">authors cited</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$ne</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Richie</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}},<br /> ... {$push : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">authors cited</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Richie</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> <span style="line-height:19px;">数组修饰符$addToSet可以达到相同的效果,假设我们有个用户document,用户有好几个email<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@gmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@yahoo.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> ]<br /> }</span> </div> </div> <span style="line-height:19px;">我们可以使用$addToSet防止插入重复的值<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$addToSet</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@gmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@gmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@yahoo.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> ]<br /> }<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$addToSet</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@hotmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@gmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@yahoo.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@hotmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> ]<br /> }</span> </div> </div> <span style="line-height:19px;">$addToSet还可以和$each联合使用,一次追加多个不重复的值,这个是$ne/$push组合做不到的<br /> </span> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$addToSet</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> :<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$each</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@php.net</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@python.org</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]}}})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">)})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">emails</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@gmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@yahoo.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@hotmail.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@php.net</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">joe@python.org</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> ]<br /> }</span> </div> </div> <span style="line-height:19px;"><br /> </span></li> <li><span style="line-height:19px;">数组修饰符$pop<br /> $pop修饰符从数组的两端移除一个元素,</span>{$pop : {key : 1}}从数组末端删除一个元素,{$pop :{key : -1}}从数组起始端删除一个元素<br /> <br /> </li> <li>数组修饰符$pull<br /> $pull从数组里移除符合条件的元素<br /> 比如,我们有一个待做事项列表<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.lists.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">todo</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">dishes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">laundry</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">dry cleaning</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> 我们想把laundry删掉<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.lists.update({}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$pull</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">todo</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">laundry</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> 这样数组里就剩两项了<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.lists.find()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b2d75476cc613d5ee930164</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">todo</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">dishes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">dry cleaning</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> ]<br /> }</span> </div> </div> $pull会删除所有的匹配元素<br /> <br /> </li> <li>按照位置操作数组的值<br /> 有两种方式:一是按照位置,二是使用位置操作符($符号)<br /> 我们先看第一种用法,位置是从0开始索引的,我们可以使用这个索引就好像它是数组的一个属性一样<br /> 假设我们有一篇博客,带有一些评论<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne()<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b329a216cc613d5ee930192</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">content</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">...</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comment</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">good post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">author</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">John</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">votes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">0</span> <span style="line-height:1.5;"><br /> },<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comment</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">i thought it was too short</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">author</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Claire</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">votes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;"><br /> },<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">comment</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">free watches</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">author</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Alice</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">votes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;"><br /> }<br /> ]<br /> }</span> </div> </div> 我想给第一篇评论的投票数加1,我们就可以这样做<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">post</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : post_id},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$inc</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments.<strong>0</strong>.votes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}})</span> </div> </div> 实际上很多时候我们根本不知道这个索引是多少,我们只知道有这么个匹配的document在,我们可以使用第二种用法,使用位置操作符$,<br /> $就代表了匹配的元素的索引,如果我们想把评论里叫John的那个改成Jim,就可以这样子做<br /> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';font-size:12px;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">db.blog.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments.author</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">John</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments.<strong>$</strong>.author</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Jim</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> </li> </ul> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>4.Upsert</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这估计是作者自己造的单词,指如果存在匹配的document就更新,如果不存在匹配就插入。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">将update函数的第三个参数设为true即可,如:</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">db.analytics.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">url</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">/blog</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$inc</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">visits</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}}, </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">)</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">shell的save函数也可以达到同样的目的,如果存在就更新,如果不存在就插入。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">save函数使用一个document做参数,如果document有"_id"键就更新,如果没有就插入。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> x </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.foo.findOne()<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> x.num </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;">42</span> <span style="line-height:1.5;"><br /> </span> <span style="line-height:1.5;">42</span> <span style="line-height:1.5;"><br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.save(x)</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>5.更新多个document</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">缺省情况下,update函数只更新匹配的第一条记录,余下的不做改变,要想更新所有的匹配记录,将update函数的第4个参数设为true</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.update({birthday : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">10/13/1978</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... {$set : {gift : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Happy Birthday!</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}}, </span> <span style="line-height:1.5;color:#0000ff;">false</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">)</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>6.返回被更新的document</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">findAndModify命令的调用比普通的update要慢一些,因为它要等待服务器的响应。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">findAndModify命令适合处理队列,或者其他的原子性的get-and-set式的操作。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">假设我们有一个处理流程的collection,需要按一定的顺序执行,一个document代表了一个处理流程,如下</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">{<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : state,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : N<br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">status是个字符串,可能的值是"Ready","Running","Done".我们需要找到Ready状态优先级最高的处理流程,处理完成后把状态设为Done。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">我们查询Ready状态的所有流程,按优先级排序,把最高的那个标记为Running,然后执行处理流程,结束后把状态设为Done。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">ps </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.processes.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">).sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}).limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">).next()<br /> db.processes.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps._id}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">RUNNING</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})<br /> do_something(ps);<br /> db.processes.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps._id}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">DONE</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个算法并不好,会产生资源竞争。假设我们有两个线程来处理,一个线程(线程A)获取了document,另一个线程(线程B)可能在A将状态设置为 Running之前获取同一个document,然后两个线程会执行同一个处理流程。我们可以将检查status作为update的一部分来避免这个问题,不过会变得复杂:</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.processes.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}).sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}).limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">);<br /> </span> <span style="line-height:1.5;color:#0000ff;">while</span> <span style="line-height:1.5;"> ((ps </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> cursor.next()) </span> <span style="line-height:1.5;">!=</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;">) {<br /> ps.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps._id, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">RUNNING</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}});<br /> </span> <span style="line-height:1.5;color:#0000ff;"> var</span> <span style="line-height:1.5;"> lastOp </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.runCommand({getlasterror : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">});<br /> </span> <span style="line-height:1.5;color:#0000ff;"> if</span> <span style="line-height:1.5;"> (lastOp.n </span> <span style="line-height:1.5;">==</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">) {<br /> do_something(ps);<br /> db.processes.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps._id}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">DONE</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})<br /> </span> <span style="line-height:1.5;color:#0000ff;"> break</span> <span style="line-height:1.5;">;<br /> }<br /> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.processes.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}).sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}).limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">);<br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这样有另外一个问题,依赖于运行时,一个线程可能处理完所有的工作然后结束,而另一个线程无用的跟在后边。线程A总是能获取处理流程,线程B试图获取同一个处理流程,然后失败,然后看着A完成所有的工作。这种情况就非常适合使用findAndModify命令,findAndModify命令在同一个操作里返回项目并更新它。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> ps </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.runCommand({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">findAndModify</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">processes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">query</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">sort</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">},<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">update</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">RUNNING</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">ok</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">value</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b3e7a18005cab32be6291f7</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }<br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">Note:返回的document中的状态仍然是Ready,在修饰符生效之前,document已经返回了。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">执行find查看就可以看到status被设置为了Running</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.processes.findOne({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps.value._id})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4b3e7a18005cab32be6291f7</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">RUNNING</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">所以我们的程序应该是这个样子:</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> ps </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.runCommand({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">findAndModify</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">processes</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">query</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">READY</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">},<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">sort</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">priority</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">},<br /> ... </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">update</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">RUNNING</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}}).value<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> do_something(ps)<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.process.update({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ps._id}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">status</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">DONE</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">findAndModify命令里含有一个"update"键或"remove"键,remove表示匹配的document会被从collection里删除。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">findAndModify命令里各个key的值意义如下</p> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>findAndModify: 字符串,collection的名字</li> </ul> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>query:查询document,检索document的条件</li> </ul> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>sort:按照什么排序</li> </ul> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>update:修饰符document,如何更新匹配的document</li> </ul> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>remove:布尔值,指示是否删除document</li> </ul> <ul style="line-height:20px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;margin-left:45px;font-size:13px;"> <li>new:布尔值,指示返回的document是更新前的还是更新后的,缺省为更新前的。</li> </ul> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>7.密西西比河此岸的最快书写</strong>(The Fastest Write This Side of Mississippi)</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">本章节所关注的三个操作(insert,update,remove)看起来都是瞬发的,因为它们不会等待服务器的响应。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这并不是异步,应当被看作是"fire-and-forget"型的函数:客户端向服务器发送了document然后就继续自己的事情,客户端从不会收到一个响应诸如“ok,我收到你的消息啦”或者“不ok,你得给我重新发送一次”之类的东西。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">安全操作</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这些操作的安全版本就是在执行操作之后立刻调用getLastError命令。驱动会等待服务器响应并做相应的处理,通常是抛出一个异常,开发人员可以捕获然后处理。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">操作成功之后,getLastError也会返回一些信息,比如update或remove,信息里包含了受影响的document数。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">请求和连接</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">数据库为每个到mongoDB的连接建立一个请求队列,客户端发出一个请求,就会被放到队列的尾部。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">注意是一个连接一个队列,如果我们打开两个shell,那么我们有了两个连接,如果我们在一个shell里执行插入,然后在另一个shell里执行查询,有可能得不到刚才插入的document。在同一个shell里执行插入和查询不会有问题,插入的document会被返回。这种情况在使用 Ruby,Python 和Jave驱动时尤其值得注意,因为它们都是用连接池,出于性能上的考虑,这些驱动打开多个连接,然后将请求分配给它们。不过它们本身都有自己的机制,保证一系列的请求会使用一个连接来处理。</p> </div>