MongoDB数据修改总结

1.前言

最近在学习MongoDB,数据修改这一部分的内容较多,命令比较繁琐,所以将一些常用的修改命令总结在这篇博客中,方便今后学习的查阅。

2.命令总结

1). insert()

db.collection.insert(x)  x就是要更新的对象,只能是单条记录,如:

[plain]  view plain copy
  1. db.collection.insert({_id:1,name:"test",count:1})  

当需要批量插入的时候,可以在shell中使用for循环,如:

[plain]  view plain copy
  1. for(var i=0;i<16;i++){   
  2.      db.mytest.insert({_id:i,name:"test"+i,count:i})   
  3. }  
此时如果用find()命令查询插入的数据,结果是这样的:
[plain]  view plain copy
  1. > db.mytest.find()  

{ "_id" : 0, "name" : "test0", "count" : 0 }
{ "_id" : 1, "name" : "test1", "count" : 1 }
{ "_id" : 2, "name" : "test2", "count" : 2 }
{ "_id" : 3, "name" : "test3", "count" : 3 }
{ "_id" : 4, "name" : "test4", "count" : 4 }
{ "_id" : 5, "name" : "test5", "count" : 5 }
{ "_id" : 6, "name" : "test6", "count" : 6 }
{ "_id" : 7, "name" : "test7", "count" : 7 }
{ "_id" : 8, "name" : "test8", "count" : 8 }
{ "_id" : 9, "name" : "test9", "count" : 9 }
{ "_id" : 10, "name" : "test10", "count" : 10 }
{ "_id" : 11, "name" : "test11", "count" : 11 }
{ "_id" : 12, "name" : "test12", "count" : 12 }
{ "_id" : 13, "name" : "test13", "count" : 13 }
{ "_id" : 14, "name" : "test14", "count" : 14 }
{ "_id" : 15, "name" : "test15", "count" : 15 }


2). update()

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,就把按条件查出来多条记录全部更新。

几个查询例子如下:

db.mytest.update({count:{$gt:1}},{$set:{name:"ok"}})                                  只更新第一条记录

db.mytest.update({count:{$gt:3}},{$set:{name:"ok"}},false,true)                  大于3的全部更新了

db.mytest.update({count:{$gt:4}},{$set:{name:"ok123"}},true,false)            只更新了一条

db.mytest.update({count:{$gt:6}},{$set:{name:"ok123"}},true,true)              大于6的全部更新了


3). save()

db.collection.save(x) x是要插入的对象,效果与上面的insert命令一样。save与insert的区别是这样的:

在进行插入数据的操作中,当遇到_id相同的情况下,save完成保存操作,insert则会保存;即_id相同情况下,save相当于更新操作。


下面是一些MongoDB的更新操作符


4). $inc

用法:{$inc:{field:value}}   意思是对一个数字字段field增加value:

[plain]  view plain copy
  1. > db.mytest.find({_id:1})       
  2. { "_id" : 1, "name" : "test1", "count" : 1 }  
  3. > db.mytest.update({_id:1},{$inc:{count:1}})  
  4. > db.mytest.find({_id:1})  
  5. { "_id" : 1, "name" : "test1", "count" : 2 }  //count字段加1  


value的值也可以为负,就相当于减一个值:

[plain]  view plain copy
  1. > db.mytest.update({_id:1},{$inc:{count:-2}})  
  2. > db.mytest.find({_id:1})  
  3. { "_id" : 1, "name" : "test1", "count" : 0 }  //值从2减到0  

5). $set命令
用法:{$set:{field:value}}

相当于在关系型数据库中sql的set field=value,全部数据类型都支持$set操作

[plain]  view plain copy
  1. > db.mytest.update({_id:1},{$set:{count:111}})  
  2. > db.mytest.find({_id:1})  
  3. { "_id" : 1, "name" : "test1", "count" : 111 }   //修改数值型  
  4. > db.mytest.update({_id:1},{$set:{name:"MongoDB"}})  
  5. > db.mytest.find({_id:1})  
  6. { "_id" : 1, "count" : 111, "name" : "MongoDB" }   //修改字符型  

6). $unset

用法:{$unset:{field:1}}

[plain]  view plain copy
  1. > db.mytest.find({_id:1})  
  2. { "_id" : 1, "count" : 111, "name" : "MongoDB" }  
  3. > db.mytest.update({_id:1},{$unset:{name:1}})  
  4. > db.mytest.find({_id:1})  
  5. { "_id" : 1, "count" : 111 }  //删除了字段name  

7). $push

用法:{$push:{field:value}}

把value追加到field中取,field一定是数据类型才行,如果field不存在,会新增一个数组类型加进去:

[plain]  view plain copy
  1. > db.mytest.update({_id:15},{$set:{array:["aaa","bbb"]}})  
  2. > db.mytest.find({_id:15})  
  3. { "_id" : 15, "array" : [  "aaa",  "bbb" ], "count" : 15, "name" : "ok123" }  
使用push追加数据:

[plain]  view plain copy
  1. > db.mytest.update({_id:15},{$push:{array:"ccc"}})  
  2. > db.mytest.find({_id:15})  
  3. { "_id" : 15, "array" : [  "aaa",  "bbb",  "ccc" ], "count" : 15, "name" : "ok123" }  
push一次只能追加一个值,如果需要追加多个值,则需要使用 $pushAll:
[plain]  view plain copy
  1. > db.mytest.update({_id:15},{$pushAll:{array:["ddd","eee","fff"]}})  
  2. > db.mytest.find({_id:15})  
  3. { "_id" : 15, "array" : [  "aaa",  "bbb",  "ccc",  "ddd",  "eee",  "fff" ], "count" : 15, "name" : "ok123" }  

8). $addToSet

用法:{$addToSet:{field:value}}

增加一个值到数组内,而且只有当这个值不在数组内才增加:

[plain]  view plain copy
  1. > db.mytest.update({_id:15},{$addToSet:{array:"123"}})  
  2. > db.mytest.find({_id:15})  
  3. { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }  
  4. > db.mytest.update({_id:15},{$addToSet:{array:"aaa"}})  
  5. > db.mytest.find({_id:15})  
  6. { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }  

9). $pop

删除数组内的一个值,删除最后一个值:{$pop:{field:1}} ,删除第一个值:{$pop:{field:-1}}

[plain]  view plain copy
  1. > db.mytest.find({_id:15})  
  2. { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }  
  3. > db.mytest.update({_id:15},{$pop:{array:1}})  
  4. > db.mytest.find({_id:15})  
  5. { "_id" : 15, "array" : [  "aaa",  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15, "name" : "ok123" }  

10). $pull

用法:$pull:{field:value}   从数组中删除一个等于value的值:

[plain]  view plain copy
  1. > db.mytest.find({_id:15})  
  2. { "_id" : 15, "array" : [  "aaa",  "bbb" ], "array2" : [  "mmm",  "nnn" ], "coun  
  3. t" : 15, "name" : "ok123" }  
  4. > db.mytest.update({_id:15},{$pull:{array:"aaa"}})  
  5. > db.mytest.find({_id:15})  
  6. { "_id" : 15, "array" : [  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15,  
  7.  "name" : "ok123" }  

11). $pullAll

用法同$pull,可以一次删除数组内的多个值:

[plain]  view plain copy
  1. > db.mytest.find({_id:15})  
  2. { "_id" : 15, "array" : [  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15,"name" : "ok123" }  
  3. > db.mytest.update({_id:15},{$pullAll:{array2:["mmm","nnn"]}})  
  4. > db.mytest.find({_id:15})  
  5. { "_id" : 15, "array" : [  "bbb" ], "array2" : [ ], "count" : 15, "name" : "ok123" }  

12). $

可以理解为数组定位器,看一个官方文档的例子:

[plain]  view plain copy
  1. > t.find()  
  2. { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }  
  3. > t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}})  
  4. > t.find()  
  5. { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }  

需要注意的是,$只会找到第一条数组项,后面的就不管了:

[plain]  view plain copy
  1. > db.mytest.find({_id:16})  
  2. { "_id" : 16, "x" : [  1,  2,  3,  1 ] }  
  3. > db.mytest.update({x:1},{$inc:{"x.$":1}})  
  4. > db.mytest.find({_id:16})  
  5. { "_id" : 16, "x" : [  2,  2,  3,  1 ] }  


还有一点需要注意,当$配合$unset使用的时候,会留下一个null的数组项,这个问题可以使用{$pull:{x:null}}解决:

[plain]  view plain copy
  1. > db.mytest.find({_id:16})  
  2. { "_id" : 16, "x" : [  2,  2,  3,  1 ] }  
  3. > db.mytest.update({x:3},{$unset:{"x.$":1}})  
  4. > db.mytest.find({_id:16})  
  5. { "_id" : 16, "x" : [  2,  2,  null,  1 ] }  
  6. > db.mytest.update({_id:16},{$pull:{x:null}})  
  7. > db.mytest.find({_id:16})  
  8. { "_id" : 16, "x" : [  2,  2,  1 ] }  
原文地址:点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值