您的位置:首页 > 博客中心 > 数据库 >

MongoDB数据修改总结

时间:2022-03-13 23:36

MongoDB数据修改总结
2013-10-26 10:08:30          来源:huhui的专栏   
 
MongoDB数据修改总结   1.前言 最近在学习MongoDB,数据修改这一部分的内容较多,命令比较繁琐,所以将一些常用的修改命令总结在这篇博客中,方便今后学习的查阅。 2.命令总结 1). insert() db.collection.insert(x)  x就是要更新的对象,只能是单条记录,如: [plain] db.collection.insert({_id:1,name:"test",count:1})  当需要批量插入的时候,可以在shell中使用for循环,如: [plain] for(var i=0;i<16;i++){        db.mytest.insert({_id:i,name:"test"+i,count:i})   }  此时如果用find()命令查询插入的数据,结果是这样的: [plain] > 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] > db.mytest.find({_id:1})       { "_id" : 1, "name" : "test1", "count" : 1 }  > db.mytest.update({_id:1},{$inc:{count:1}})  > db.mytest.find({_id:1})  { "_id" : 1, "name" : "test1", "count" : 2 }  //count字段加1    value的值也可以为负,就相当于减一个值: [plain] > db.mytest.update({_id:1},{$inc:{count:-2}})  > db.mytest.find({_id:1})  { "_id" : 1, "name" : "test1", "count" : 0 }  //值从2减到0    5). $set命令 用法:{$set:{field:value}} 相当于在关系型中sql的set field=value,全部数据类型都支持$set操作 [plain] > db.mytest.update({_id:1},{$set:{count:111}})  > db.mytest.find({_id:1})  { "_id" : 1, "name" : "test1", "count" : 111 }   //修改数值型  > db.mytest.update({_id:1},{$set:{name:"MongoDB"}})  > db.mytest.find({_id:1})  { "_id" : 1, "count" : 111, "name" : "MongoDB" }   //修改字符型    6). $unset 用法:{$unset:{field:1}} [plain] > db.mytest.find({_id:1})  { "_id" : 1, "count" : 111, "name" : "MongoDB" }  > db.mytest.update({_id:1},{$unset:{name:1}})  > db.mytest.find({_id:1})  { "_id" : 1, "count" : 111 }  //删除了字段name    7). $push 用法:{$push:{field:value}} 把value追加到field中取,field一定是数据类型才行,如果field不存在,会新增一个数组类型加进去: [plain] > db.mytest.update({_id:15},{$set:{array:["aaa","bbb"]}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb" ], "count" : 15, "name" : "ok123" }  使用push追加数据: [plain] > db.mytest.update({_id:15},{$push:{array:"ccc"}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb",  "ccc" ], "count" : 15, "name" : "ok123" }  push一次只能追加一个值,如果需要追加多个值,则需要使用$pushAll: [plain] > db.mytest.update({_id:15},{$pushAll:{array:["ddd","eee","fff"]}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb",  "ccc",  "ddd",  "eee",  "fff" ], "count" : 15, "name" : "ok123" }    8). $addToSet 用法:{$addToSet:{field:value}} 增加一个值到数组内,而且只有当这个值不在数组内才增加: [plain] > db.mytest.update({_id:15},{$addToSet:{array:"123"}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }  > db.mytest.update({_id:15},{$addToSet:{array:"aaa"}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }    9). $pop 删除数组内的一个值,删除最后一个值:{$pop:{field:1}} ,删除第一个值:{$pop:{field:-1}} [plain] > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb",  "123" ], "array2" : [  "mmm",  "nnn"], "count" : 15, "name" : "ok123" }  > db.mytest.update({_id:15},{$pop:{array:1}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15, "name" : "ok123" }    10). $pull 用法:$pull:{field:value}   从数组中删除一个等于value的值: [plain] > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "aaa",  "bbb" ], "array2" : [  "mmm",  "nnn" ], "coun  t" : 15, "name" : "ok123" }  > db.mytest.update({_id:15},{$pull:{array:"aaa"}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15,  "name" : "ok123" }    11). $pullAll 用法同$pull,可以一次删除数组内的多个值: [plain] > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "bbb" ], "array2" : [  "mmm",  "nnn" ], "count" : 15,"name" : "ok123" }  > db.mytest.update({_id:15},{$pullAll:{array2:["mmm","nnn"]}})  > db.mytest.find({_id:15})  { "_id" : 15, "array" : [  "bbb" ], "array2" : [ ], "count" : 15, "name" : "ok123" }    12). $ 可以理解为数组定位器,看一个官方文档的例子: [plain] > t.find()  { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }  > t.update( {‘comments.by‘:‘joe‘}, {$inc:{‘comments.$.votes‘:1}})  > t.find()  { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }  需要注意的是,$只会找到第一条数组项,后面的就不管了: [plain] > db.mytest.find({_id:16})  { "_id" : 16, "x" : [  1,  2,  3,  1 ] }  > db.mytest.update({x:1},{$inc:{"x.$":1}})  > db.mytest.find({_id:16})  { "_id" : 16, "x" : [  2,  2,  3,  1 ] }    还有一点需要注意,当$配合$unset使用的时候,会留下一个null的数组项,这个问题可以使用{$pull:{x:null}}解决: [plain] > db.mytest.find({_id:16})  { "_id" : 16, "x" : [  2,  2,  3,  1 ] }  > db.mytest.update({x:3},{$unset:{"x.$":1}})  > db.mytest.find({_id:16})  { "_id" : 16, "x" : [  2,  2,  null,  1 ] }  > db.mytest.update({_id:16},{$pull:{x:null}})  > db.mytest.find({_id:16})  { "_id" : 16, "x" : [  2,  2,  1 ] }   

热门排行

今日推荐

热门手游