免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2782 | 回复: 0
打印 上一主题 下一主题

[MongoDB] mongodb update,save操作说明 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 02:27 |只看该作者 |倒序浏览
<DIV>2011-01-07<BR>mongodb update,save操作说明 博客分类: 编程<BR>MongoDBSQL编程PHPBlog.原文地址:<a href="http://hi.baidu.com/farmerluo/blog/item/9a23cb13a819bb2fdd540188.html" target="_blank">http://hi.baidu.com/farmerluo/blog/item/9a23cb13a819bb2fdd540188.html</A></DIV>
<DIV>&nbsp;</DIV>
<DIV>MongoDB update数据语法<BR>2010年05月13日 星期四 16:16<BR>在前面的文章“mongodb 查询的语法 ”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方便以后自己用到的时候查阅:</DIV>
<DIV>注:在这篇文章及上篇文章内讲的语法介绍都是在mongodb shell环境内的,和真正运用语言编程(如java,php等)使用时,在使用方法上会有一些差别,但语法(如查询条件,$in,$inc等)是一样的。</DIV>
<DIV>本文是参考官方文档 来介绍的,之所以有官方文档还要在这介绍,一方面是就当翻译,毕竟每次要用时去看英文文档比较累,第二是官方文档讲解比较简单,有时光看官方文档不好理解,我在实际操作的情况下可以做些补充。</DIV>
<DIV>好了,不多说了,下面正式开始:</DIV>
<DIV>mongodb更新有两个命令:</DIV>
<DIV>1).update()命令 </DIV>
<DIV>db.collection.update( criteria, objNew, upsert, multi )</DIV>
<DIV>criteria : update的查询条件,类似sql update查询内where后面的<BR>objNew&nbsp;&nbsp; : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的<BR>upsert&nbsp;&nbsp; : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。<BR>multi&nbsp;&nbsp;&nbsp; : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。</DIV>
<DIV>例:<BR>db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录<BR>db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了<BR>db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加进去了第一条<BR>db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加进去了<BR>db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了<BR>db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一条</DIV>
<DIV>2).save()命令 </DIV>
<DIV>db.collection.save( x )</DIV>
<DIV>x就是要更新的对象,只能是单条记录。</DIV>
<DIV>如果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。</DIV>
<DIV>例:<BR>db.test0.save({count:40,test1:"OK"}); #_id系统会生成<BR>db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0内有_id等于40的,会替换,否则插入。</DIV>
<DIV><BR>mongodb的更新操作符:</DIV>
<DIV>1) $inc </DIV>
<DIV>用法:{ $inc : { field : value } }</DIV>
<DIV>意思对一个数字字段field增加value,例:</DIV>
<DIV>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 16, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $inc : { "count" : 1 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 17, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $inc : { "count" : 2 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 19, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $inc : { "count" : -1 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }</DIV>
<DIV><BR>2) $set </DIV>
<DIV>用法:{ $set : { field : value } }</DIV>
<DIV>就是相当于sql的set field = value,全部数据类型都支持$set。例:<BR>&gt; db.test0.update( { "_id" : 15 } , { $set : { "test1" : "testv1","test2" : "testv2","test3" : "testv3","test4" : "testv4" } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "testv1", "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>3) $unset </DIV>
<DIV>用法:{ $unset : { field : 1} }</DIV>
<DIV>顾名思义,就是删除字段了。例:<BR>&gt; db.test0.update( { "_id" : 15 } , { $unset : { "test1":1 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $unset : { "test2": 0 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $unset : { "test3":asdfasf } } );<BR>Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test" } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>没看出field : 1里面的1是干什么用的,反正只要有东西就行。</DIV>
<DIV><BR>4) $push </DIV>
<DIV><BR>用法:{ $push : { field : value } }</DIV>
<DIV>把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去。例:</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $set : { "test1" : ["aaa","bbb"] } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $push : { "test1": "ccc" } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $push : { "test2": "ccc" } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $push : { "test1": ["ddd","eee"] } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }5) $pushAll </DIV>
<DIV>用法:{ $pushAll : { field : value_array } }</DIV>
<DIV>同$push,只是一次可以追加多个值到一个数组字段内。例:</DIV>
<DIV>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $pushAll : { "test1": ["fff","ggg"] } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV>6)&nbsp; $addToSet </DIV>
<DIV><BR>用法:{ $addToSet : { field : value } }</DIV>
<DIV>增加一个值到数组内,而且只有当这个值不在数组内才增加。例:<BR>&gt; db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "aaa",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bbb",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555"<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }<BR>&gt; db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "aaa",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bbb",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555"<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }<BR>&gt; db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]&nbsp; } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "aaa",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bbb",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }<BR>&gt; db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]&nbsp; } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "aaa",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bbb",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "555"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV><BR>7) $pop</DIV>
<DIV><BR>删除数组内的一个值</DIV>
<DIV>用法:<BR>删除最后一个值:{ $pop : { field : 1&nbsp; } }删除第一个值:{ $pop : { field : -1&nbsp; } }</DIV>
<DIV>注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用,例:<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bbb",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444"<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }<BR>&gt; db.test0.update( { "_id" : 15 } , { $pop : { "test1": -1 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ccc",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ddd",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "eee"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "fff",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "ggg",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "111",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "222"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "444"<BR>], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }<BR>&gt; db.test0.update( { "_id" : 15 } , { $pop : { "test1": 1 } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",<BR>"test5" : "OK" }</DIV>
<DIV>8) $pull</DIV>
<DIV><BR>用法:$pull : { field : value } }</DIV>
<DIV>从数组field内删除一个等于value值。例:<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",<BR>"test5" : "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"<BR>&nbsp;: "OK" }</DIV>
<DIV>9) $pullAll</DIV>
<DIV><BR>用法:{ $pullAll : { field : value_array } }</DIV>
<DIV>同$pull,可以一次删除数组内的多个值。例:<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"<BR>&nbsp;: "OK" }</DIV>
<DIV>&gt; db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );<BR>&gt; db.test0.find( { "_id" : 15 } );<BR>{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ [ "ddd", "eee" ], [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }</DIV>
<DIV><BR>10) $ 操作符</DIV>
<DIV><BR>$是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:</DIV>
<DIV>&gt; t.find()<BR>{ "_id"<BR>&nbsp;: ObjectId("4b97e62bf1d8c7152c9ccb74"<BR>), "title"<BR>&nbsp;: "ABC"<BR>,&nbsp; "comments"<BR>&nbsp;: [ { "by"<BR>&nbsp;: "joe"<BR>, "votes"<BR>&nbsp;: 3 }, { "by"<BR>&nbsp;: "jane"<BR>, "votes"<BR>&nbsp;: 7 } ] }</DIV>
<DIV>&gt; t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false<BR>, true<BR>&nbsp;)</DIV>
<DIV>&gt; t.find()<BR>{ "_id"<BR>&nbsp;: ObjectId("4b97e62bf1d8c7152c9ccb74"<BR>), "title"<BR>&nbsp;: "ABC"<BR>,&nbsp; "comments"<BR>&nbsp;: [ { "by"<BR>&nbsp;: "joe"<BR>, "votes"<BR>&nbsp;: 4 }, { "by"<BR>&nbsp;: "jane"<BR>, "votes"<BR>&nbsp;: 7 } ] }</DIV>
<DIV>需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子:</DIV>
<DIV>&gt; t.find();<BR>{ "_id"<BR>&nbsp;: ObjectId("4b9e4a1fc583fa1c76198319"<BR>), "x"<BR>&nbsp;: [ 1, 2, 3, 2 ] }<BR>&gt; t.update({x: 2}, {$inc: {"x.$"<BR>: 1}}, false<BR>, true<BR>);<BR>&gt; t.find();</DIV>
<DIV>还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例:<BR>&gt; t.insert({x: [1,2,3,4,3,2,3,4]})<BR>&gt; t.find()<BR>{ "_id"<BR>&nbsp;: ObjectId("4bde2ad3755d00000000710e"<BR>), "x"<BR>&nbsp;: [ 1, 2, 3, 4, 3, 2, 3, 4 ] }<BR>&gt; t.update({x:3}, {$unset:{"x.$"<BR>:1}})<BR>&gt; t.find()<BR>{ "_id"<BR>&nbsp;: ObjectId("4bde2ad3755d00000000710e"<BR>), "x"<BR>&nbsp;: [ 1, 2, null<BR>, 4, 3, 2, 3, 4 ] }</DIV>
<DIV>{ "_id"<BR>&nbsp;: ObjectId("4b9e4a1fc583fa1c76198319"<BR>), "x"<BR>&nbsp;: [ 1, 3, 3, 2 ] }</DIV>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP