在前面的文章“mongodb 查询的语法 ”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方便以后自己用到的时候查阅:
注:在这篇文章及上篇文章内讲的语法介绍都是在mongodb shell环境内的,和真正运用语言编程(如java,php等)使用时,在使用方法上会有一些差别,但语法(如查询条件,$in,$inc等)是一样的。
本文是参考官方文档 来介绍的,之所以有官方文档还要在这介绍,一方面是就当翻译,毕竟每次要用时去看英文文档比较累,第二是官方文档讲解比较简单,有时光看官方文档不好理解,我在实际操作的情况下可以做些补充。
好了,不多说了,下面正式开始:
mongodb更新有两个命令:
1).update()命令
 
 
  
  1. db.collection.update( criteria, objNew, upsert, multi ) 
criteria : update的查询条件,类似sql update查询内where后面的
objNew   : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert   : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi    : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
例:
 
 
  
  1. db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录 
  2. db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了 
  3. db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加进去了第一条 
  4. db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加进去了 
  5. db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了 
  6. db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一条 
2).save()命令
 
 
  
  1. db.collection.save( x ) 
x就是要更新的对象,只能是单条记录。
如果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。
例:
 
 
  
  1. db.test0.save({count:40,test1:"OK"}); #_id系统会生成 
  2. db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0内有_id等于40的,会替换,否则插入。 

mongodb的更新操作符:
1) $inc
用法:{ $inc : { field : value } }
意思对一个数字字段field增加value,例:
 
 
  
  1. > db.test0.find( { "_id" : 15 } ); 
  2. "_id" : { "floatApprox" : 15 }, "count" : 16, "test1" : "TESTTEST""test2" : "OK""test3" : "TESTTEST""test4" : "OK""test5" : "OK" } 
  3. > db.test0.update( { "_id" : 15 } , { $inc : { "count" : 1 } } ); 
  4. > db.test0.find( { "_id" : 15 } ); 
  5. "_id" : { "floatApprox" : 15 }, "count" : 17, "test1" : "TESTTEST""test2" : "OK""test3" : "TESTTEST""test4" : "OK""test5" : "OK" } 
  6. > db.test0.update( { "_id" : 15 } , { $inc : { "count" : 2 } } ); 
  7. > db.test0.find( { "_id" : 15 } ); 
  8. "_id" : { "floatApprox" : 15 }, "count" : 19, "test1" : "TESTTEST""test2" : "OK""test3" : "TESTTEST""test4" : "OK""test5" : "OK" } 
  9. > db.test0.update( { "_id" : 15 } , { $inc : { "count" : -1 } } ); 
  10. > db.test0.find( { "_id" : 15 } ); 
  11. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "TESTTEST""test2" : "OK""test3" : "TESTTEST""test4" : "OK""test5" : "OK" } 

2) $set
用法:{ $set : { field : value } }
就是相当于sql的set field = value,全部数据类型都支持$set。例:
 
 
  
  1. > db.test0.update( { "_id" : 15 } , { $set : { "test1" : "testv1","test2" : "testv2","test3" : "testv3","test4" : "testv4" } } ); 
  2. > db.test0.find( { "_id" : 15 } ); 
  3. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "testv1""test2" : "testv2""test3" : "testv3""test4" : "testv4""test5" : "OK" } 
3) $unset
用法:{ $unset : { field : 1} }
顾名思义,就是删除字段了。例:
 
 
  
  1. > db.test0.update( { "_id" : 15 } , { $unset : { "test1":1 } } ); 
  2. > db.test0.find( { "_id" : 15 } ); 
  3. "_id" : { "floatApprox" : 15 }, "count" : 18, "test2" : "testv2""test3" : "testv3""test4" : "testv4""test5" : "OK" } 
  4. > db.test0.update( { "_id" : 15 } , { $unset : { "test2": 0 } } ); 
  5. > db.test0.find( { "_id" : 15 } ); 
  6. "_id" : { "floatApprox" : 15 }, "count" : 18, "test3" : "testv3""test4" : "testv4""test5" : "OK" } 
  7. > db.test0.update( { "_id" : 15 } , { $unset : { "test3":asdfasf } } ); 
  8. Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0 
  9. > db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test" } } ); 
  10. > db.test0.find( { "_id" : 15 } ); 
  11. "_id" : { "floatApprox" : 15 }, "count" : 18, "test4" : "testv4""test5" : "OK" } 
没看出field : 1里面的1是干什么用的,反正只要有东西就行。

4) $push

用法:{ $push : { field : value } }
把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去。例:
 
 
  
  1. > db.test0.update( { "_id" : 15 } , { $set : { "test1" : ["aaa","bbb"] } } ); 
  2. > db.test0.find( { "_id" : 15 } ); 
  3. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb" ], "test4" : "testv4""test5" : "OK" } 
  4. > db.test0.update( { "_id" : 15 } , { $push : { "test1""ccc" } } ); 
  5. > db.test0.find( { "_id" : 15 } ); 
  6. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb""ccc" ], "test4" : "testv4""test5" : "OK" } 
  7. > db.test0.update( { "_id" : 15 } , { $push : { "test2""ccc" } } ); 
  8. > db.test0.find( { "_id" : 15 } ); 
  9. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb""ccc" ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  10. > db.test0.update( { "_id" : 15 } , { $push : { "test1": ["ddd","eee"] } } ); 
  11. > db.test0.find( { "_id" : 15 } ); 
  12. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb""ccc", [ "ddd""eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" }5) $pushAll 
用法:{ $pushAll : { field : value_array } }
同$push,只是一次可以追加多个值到一个数组字段内。例:
 
 
  
  1. > db.test0.find( { "_id" : 15 } ); 
  2. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb""ccc", [ "ddd""eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  3. > db.test0.update( { "_id" : 15 } , { $pushAll : { "test1": ["fff","ggg"] } } ); 
  4. > db.test0.find( { "_id" : 15 } ); 
  5. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa""bbb""ccc", [ "ddd""eee" ], "fff""ggg" ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
6)  $addToSet

用法:{ $addToSet : { field : value } }
增加一个值到数组内,而且只有当这个值不在数组内才增加。例:
 
 
  
  1. > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } ); 
  2. > db.test0.find( { "_id" : 15 } ); 
  3. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  4.         "aaa"
  5.         "bbb"
  6.         "ccc"
  7.         [ 
  8.                 "ddd"
  9.                 "eee" 
  10.         ], 
  11.         "fff"
  12.         "ggg"
  13.         [ 
  14.                 "111"
  15.                 "222" 
  16.         ], 
  17.         "444"
  18.         "555" 
  19. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  20. > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } ); 
  21. > db.test0.find( { "_id" : 15 } ); 
  22. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  23.         "aaa"
  24.         "bbb"
  25.         "ccc"
  26.         [ 
  27.                 "ddd"
  28.                 "eee" 
  29.         ], 
  30.         "fff"
  31.         "ggg"
  32.         [ 
  33.                 "111"
  34.                 "222" 
  35.         ], 
  36.         "444"
  37.         "555" 
  38. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  39. > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]  } } ); 
  40. > db.test0.find( { "_id" : 15 } ); 
  41. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  42.         "aaa"
  43.         "bbb"
  44.         "ccc"
  45.         [ 
  46.                 "ddd"
  47.                 "eee" 
  48.         ], 
  49.         "fff"
  50.         "ggg"
  51.         [ 
  52.                 "111"
  53.                 "222" 
  54.         ], 
  55.         "444"
  56.         "555"
  57.         [ 
  58.                 "444"
  59.                 "555" 
  60.         ] 
  61. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  62. > db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]  } } ); 
  63. > db.test0.find( { "_id" : 15 } ); 
  64. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  65.         "aaa"
  66.         "bbb"
  67.         "ccc"
  68.         [ 
  69.                 "ddd"
  70.                 "eee" 
  71.         ], 
  72.         "fff"
  73.         "ggg"
  74.         [ 
  75.                 "111"
  76.                 "222" 
  77.         ], 
  78.         "444"
  79.         "555"
  80.         [ 
  81.                 "444"
  82.                 "555" 
  83.         ] 
  84. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 

7) $pop

删除数组内的一个值
用法:
删除最后一个值:{ $pop : { field : 1  } }删除第一个值:{ $pop : { field : -1  } }
注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用,例:
 
 
  
  1. > db.test0.find( { "_id" : 15 } ); 
  2. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  3.         "bbb"
  4.         "ccc"
  5.         [ 
  6.                 "ddd"
  7.                 "eee" 
  8.         ], 
  9.         "fff"
  10.         "ggg"
  11.         [ 
  12.                 "111"
  13.                 "222" 
  14.         ], 
  15.         "444" 
  16. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  17. > db.test0.update( { "_id" : 15 } , { $pop : { "test1": -1 } } ); 
  18. > db.test0.find( { "_id" : 15 } ); 
  19. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ 
  20.         "ccc"
  21.         [ 
  22.                 "ddd"
  23.                 "eee" 
  24.         ], 
  25.         "fff"
  26.         "ggg"
  27.         [ 
  28.                 "111"
  29.                 "222" 
  30.         ], 
  31.         "444" 
  32. ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 
  33. > db.test0.update( { "_id" : 15 } , { $pop : { "test1": 1 } } ); 
  34. > db.test0.find( { "_id" : 15 } ); 
  35. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd""eee" ], "fff""ggg", [ "111""222" ] ], "test2" : [ "ccc" ], "test4" : "testv4"
  36. "test5" : "OK" } 
8) $pull

用法:$pull : { field : value } }
从数组field内删除一个等于value值。例:
 
 
  
  1. > db.test0.find( { "_id" : 15 } ); 
  2. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd""eee" ], "fff""ggg", [ "111""222" ] ], "test2" : [ "ccc" ], "test4" : "testv4"
  3. "test5" : "OK" } 
  4. > db.test0.update( { "_id" : 15 } , { $pull : { "test1""ggg" } } ); 
  5. > db.test0.find( { "_id" : 15 } ); 
  6. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd""eee" ], "fff", [ "111""222" ] ], "test2" : [ "ccc" ], "test4" : "testv4""test5" 
  7.  : "OK" } 
9) $pullAll

用法:{ $pullAll : { field : value_array } }
同$pull,可以一次删除数组内的多个值。例:
 
 
  
  1. > db.test0.find( { "_id" : 15 } ); 
  2. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd""eee" ], "fff", [ "111""222" ] ], "test2" : [ "ccc" ], "test4" : "testv4""test5" 
  3.  : "OK" } 
  4. > db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } ); 
  5. > db.test0.find( { "_id" : 15 } ); 
  6. "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ [ "ddd""eee" ], [ "111""222" ] ], "test2" : [ "ccc" ], "test4" : "testv4""test5" : "OK" } 

10) $ 操作符

$是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:
 
 
  
  1. > t.find() 
  2. "_id" 
  3.  : ObjectId("4b97e62bf1d8c7152c9ccb74" 
  4. ), "title" 
  5.  : "ABC" 
  6. ,  "comments" 
  7.  : [ { "by" 
  8.  : "joe" 
  9. "votes" 
  10.  : 3 }, { "by" 
  11.  : "jane" 
  12. "votes" 
  13.  : 7 } ] } 
  14. > t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false 
  15. true 
  16.  ) 
  17. > t.find() 
  18. "_id" 
  19.  : ObjectId("4b97e62bf1d8c7152c9ccb74" 
  20. ), "title" 
  21.  : "ABC" 
  22. ,  "comments" 
  23.  : [ { "by" 
  24.  : "joe" 
  25. "votes" 
  26.  : 4 }, { "by" 
  27.  : "jane" 
  28. "votes" 
  29.  : 7 } ] } 
  30. 需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子: 
  31. > t.find(); 
  32. "_id" 
  33.  : ObjectId("4b9e4a1fc583fa1c76198319" 
  34. ), "x" 
  35.  : [ 1, 2, 3, 2 ] } 
  36. > t.update({x: 2}, {$inc: {"x.$" 
  37. : 1}}, false 
  38. true 
  39. ); 
  40. > t.find(); 
  41. 还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例: 
  42. > t.insert({x: [1,2,3,4,3,2,3,4]}) 
  43. > t.find() 
  44. "_id" 
  45.  : ObjectId("4bde2ad3755d00000000710e" 
  46. ), "x" 
  47.  : [ 1, 2, 3, 4, 3, 2, 3, 4 ] } 
  48. > t.update({x:3}, {$unset:{"x.$" 
  49. :1}}) 
  50. > t.find() 
  51. "_id" 
  52.  : ObjectId("4bde2ad3755d00000000710e" 
  53. ), "x" 
  54.  : [ 1, 2, null 
  55. , 4, 3, 2, 3, 4 ] } 
  56. "_id" 
  57.  : ObjectId("4b9e4a1fc583fa1c76198319" 
  58. ), "x" 
  59.  : [ 1, 3, 3, 2 ] }