MongoDB自学笔记12---4.4 更新文档

4.4 更新文档

Mongodb的增删查都已经学完了,只剩下更新了。在mongodb中提供了update()和save()方法用于更新一个文档,update()是使用最多的,save()在前面4.1插入文档的时候已经介绍了,这里就不再介绍了。

update语法如下:

db.collection.update(criteria,objNew,upsert,multi) 

参数说明:

criteria:设置查询条件,用于查询哪些文档需要被更新.

objNew:更新后的对象

upsert:设置为真的时候如果记录已经存在,更新它,否则新增一个记录,默认为false

multi:设置为真的时候,将会更新所有符合查询条件的文档。在mongodb中默认情况下只会更新第一条符合的文档。此方法必须与某些$开头的方法配合使用。

> db.user.find() 

{ "_id" : 1, "name" : "user1","age" : 1 } 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

>db.user.update({name:"user1"},{name:"user11"}) -->我们可以省略后两个参数 

> db.user.find() 

{ "_id" : 1, "name" : "user11" } 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

我们本想将user1改为user11,其他不变,但是我们看查询结果,mongodb将我们的整个文档更新了.所以我们如果要是使用上面那样的方式,更新文档,将会更新整个文档.这点需要注意.那么我们怎样才能只将user1,更新为user11呢?我们只要使用mongodb的$set方法即可,在后面我会讲解.

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

{ "_id" : 1, "name" : "user1","age" : 1 } 

{ "_id" : 7, "name" : "user1","age" : 7 } 

{ "_id" : 8, "name" : "user1","age" : 8 } 

> db.user.update({name:"user1"},{name:"user11"},1,0)/*只会更新一条数据,但是如果在这里我们直接将最后一个参数改为1,将会报错,因为这个参数需要与$方法联合使用  */

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

{ "_id" : 1, "name" : "user11" } 

{ "_id" : 7, "name" : "user1","age" : 7 } 

{"_id":8,"name":"user1","age":8}                                                   >db.user.update({name:"user111"},{name:"user111"},1,0)/*这时候我们更新name为user111的文档,但是没有,我们的第三个参数设置为了true,所以这时候将会插入一条新的文档,结果如下面一样  */

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

{ "_id" : 1, "name" : "user11" } 

{ "_id" : 7, "name" : "user1","age" : 7 } 

{ "_id" : 8, "name" : "user1","age" : 8 } 

{ "_id" : ObjectId("519a2c8259827a002f7e5ace"),"name" : "user111" } 

显示一下第四个参数:

>db.user.update({name:"user1"},{$set:{name:"userNew"}},0,1) 

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 }  

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

{ "_id" : 1, "name" : "user11" } 

{ "_id" : 7, "age" : 7, "name" :"userNew" } 

{ "_id" : 8, "age" : 8, "name" :"userNew" } 

{ "_id" : ObjectId("519a2c8259827a002f7e5ace"),"name" : "user111" } 

这里我们使用$set,这个就是只是去修改文档中某些值,而不是更新整个文档。

4.4.1 更新方法

         在mongodb中提供了很多与更新操作有关的方法,我们也可以叫它更新操作符。这些操作符主要可以分为两类:字段和数组。

字段组的主要包括:$inc、$rename、$setOnInsert、$set、$unset。

数组组的主要包括:$addToSet、$pop、$pullAll、$pull、$pushAll、$push、$each、$slice、$sort。

下面我们详细的介绍一下。

$inc

语法:db.collection.update({ field: value }, { $inc: { field1: amount } } );

为一个字段添加加上一个值,这个方法只能对数字操作,也就是说只能给数字加上一个值,当然这个值可以是负数。

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 2 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

> db.user.update({name:"user2"},{$inc:{age:10}}) /*将name为user2的年龄age加上10 */

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 12 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 6 } 

> db.user.update({name:"user6"},{$inc:{age:-4}}) /*将name为user6的年龄age加上-4,也就是减去4*/

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 12 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

$set

语法:db.collection.update({ field: value1 }, { $set: { field1: value2 } } );

当文档中包含该字段的时候,更新该字段值为value2,如果该文档中没有该字段,则为本文档添加一个字段file1,并为其赋值给value2

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 12 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

> db.user.update({name:"user2"},{$set:{age:20}}) /*将name为user2的age设置为20 */

> db.user.find() 

{ "_id" : 2, "name" : "user2","age" : 20 } 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

> db.user.update({name:"user2"},{$set:{sex:"nan"}})/*将name为user2的sex设置为nan,但是没有该字段,所以为该文档添加sex字段并赋值为nan */

> db.user.find() 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

{ "_id" : 2, "age" : 20, "name" :"user2", "sex" : "nan" } 

$unset

         语法:db.collection.update({ field: value1 }, { $unset: { field1: <arg> } } );

         参数arg可以使用true或者空字符串””,这样都会删除一个字段。

> db.user.find() 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

{ "_id" : 2, "age" : 20, "name" :"user2", "sex" : "nan" } 

> db.user.update({name:"user2"},{$unset:{sex:1}}) /*删除name为user2的sex字段.如果删除一个不存在的字段,并不会报错,没有什么效果  */

> db.user.find() 

{ "_id" : 3, "name" : "user3","age" : 3 } 

{ "_id" : 4, "name" : "user4","age" : 4 } 

{ "_id" : 5, "name" : "user5","age" : 5 } 

{ "_id" : 6, "name" : "user6","age" : 2 } 

{ "_id" : 2, "age" : 20, "name" :"user2" } 

$rename

语法:{$rename: { <old name1>: <newname1>, <old name2>: <new name2>, ... } }

为文档中的一个或者多个字段改名。

> db.test.find() 

{ "_id" : 1, "ary" : [ 3, 4, 5, [ 6, 7 ], 9 ]} 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

> db.test.update({_id:1},{$rename:{ary:"aryNew"}}) 

> db.test.find() 

{ "_id" : 1, "aryNew" : [ 3, 4, 5, [ 6, 7 ], 9 ]} 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

$setOnInsert

语法:db.collection.update(<query>,

                      { $setOnInsert:{ <field1>: <value1>, ... } },

                      {upsert: true }

                    )

$setOnInsert只有在upsert设置为true,并且被更新的文档不存在此集合中,需要插入一个新的文档的时候才起作用。在插入的时候会为新插入的文档添加给定的字段。

>db.user.update({name:"user1"},{_id:1,name:"user1"},1)

> db.user.find()

{ "_id" : 1, "name" : "user1" }

>db.user.update({name:"user2"},{$setOnInsert:{name:"user2",age:2}},1)

> db.user.find()

{ "_id" : 1, "name" : "user1" }

{ "_id" : ObjectId("51a02848d02692fff25c4238"),"age" : 2, "name" : "user2" }

> db.user.update({name:"user2"},{$setOnInsert:{name:"user3",age:3}},1)

> db.user.find()

{ "_id" : 1, "name" : "user1" }

{ "_id" : ObjectId("51a02848d02692fff25c4238"),"age" : 2, "name" : "user2" }

这个方法是在mongodb2.4中新加入的,具体在什么情况下使用,还不是很清楚。

$push

语法:db.collection.update(<query>,

                      { $push:{ <field>: <value> } }

                   )

将一个数字存入一个数组,分为三种情况,如果该字段存在,则直接将数字存入数组.如果该字段不存在,创建字段并且将数字插入该数组.如果更新的字段不是数组,会报错的.

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4 ] } 

{ "_id" : 2, "text" : "test" } 

> db.test.update({_id:1},{$push:{ary:5}}) -->数组存在 直接存入 

> db.test.find() 

{ "_id" : 2, "text" : "test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5 ] } 

> db.test.update({_id:2},{$push:{ary:6}}) -->数组不村子,创建数组并存入 

> db.test.find() 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5 ] } 

> db.test.update({_id:2},{$push:{text:6}})  -->更新字段存在但不是数组报错 

Cannot apply $push/$pushAll modifier to non-array 

如果我们想将多个值一起压入我们可能会将一个数组直接存入,但是这样是不对的,$push一次只会存入一个字段,代码如下:

> db.test.update({_id:1},{$push:{ary:[6,7]}}) 

> db.test.find() 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ] ]}

实现上面的功能我们可以使用下面的$pushAll

$pushAll

语法:db.collection.update({ field: value }, { $pushAll: { field1: [ value1, value2, value3 ] } } );

将多个数值一次存入数组.

> db.test.find() 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ] ]}

> db.test.update({_id:1},{$pushAll:{ary:[8,9]}}) 

> db.test.find() 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

$addToSet

语法:db.collection.update( { field: value }, {$addToSet: { field: value1 } } );

与$push功能相同讲一个数字存入数组,不同的是如果数组中有这个数字,将不会插入,只会插入新的数据,同样也会有三种情况,与$push相同.

> db.test.find() 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

> db.test.update({_id:2},{$addToSet:{ary:7}}) -->ary中没有7,插入成功 

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

{ "_id" : 2, "ary" : [ 6, 7 ], "text": "test" } 

> db.test.update({_id:2},{$addToSet:{ary:7}})  -->ary中有7,插入失败  

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

{ "_id" : 2, "ary" : [ 6, 7 ], "text": "test" } 

$pop

语法:db.collection.update( {field: value }, { $pop:{ field:,<arg> } } )

删除数组最后一个或者第一个元素。如果参数arg设置为1,删除最后一个元素,如果设置为-1,则删除第一个元素。

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

{ "_id" : 2, "ary" : [ 6, 7 ], "text": "test" } 

> db.test.update({_id:2},{$pop:{ary:1}}) 

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

$pull

语法:db.collection.update( { field: <query> },{ $pull: { field: <query> } } );

删除数组中的一个元素,如果删除的字段不是数组,会报错

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],8, 9 ] } 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

> db.test.update({_id:1},{$pull:{ary:8}}) 

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],9 ] } 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

$pullAll

语法:db.collection.update( { field: value }, {$pushAll: { field1: [ value1, value2, value3 ] } } );

删除数组中的多个值,跟pushAll与push的关系类似.

> db.test.find() 

{ "_id" : 1, "ary" : [ 1, 2, 3, 4, 5, [ 6, 7 ],9 ] } 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

> db.test.update({_id:1},{$pullAll:{ary:[1,2,8]}}) 

> db.test.find() 

{ "_id" : 1, "ary" : [ 3, 4, 5, [ 6, 7 ], 9 ]} 

{ "_id" : 2, "ary" : [ 6 ], "text" :"test" } 

$each

         $each只能和$addToSet或者$push结合使用,将多个值一次存入数组。

         语法如下:

结合$addToSet

db.collection.update( <query>,

                      {

                       $addToSet: { <field>: { $each: [ <value1>, <value2>... ] } }

                      }

                    )

结合$push:

db.collection.update( <query>,

                      {

                        $push:{ <field>: { $each: [ <value1>, <value2> ... ] } }

                      }

                    )

结合$addToSet使用的一段例子代码如下:

> db.c5.find()

{ "_id" : 1, "ary" : [ 1 ] }

> db.c5.update({_id:1},{$addToSet:{ary:{$each:[1,2,3,4]}}})

> db.c5.find()

{ "_id" : 1, "ary" : [ 1, 2, 3, 4 ] }

$slice

         语法:db.collection.update(<query>,

                      { $push: {

                                 <field>: {

                                           $each: [ <value1>, <value2>, ... ],

                                           $slice: <num>

                                          }

                               }

                      }

                    )

$slice需要和$push结合使用,截取一定长度的数组。参数num是小于或者等于0的数。如果num等于0表示的是返回的是一个空数组,如果是负数表示从后向前截取负数绝对值长度的数组。具体看下面代码:

> db.c5.find()

{ "_id" : 1, "ary" : [ 2, 3 ] }

> db.c5.update({_id:1},{$push:{ary:{$each:[1,2,3],$slice:-0}}})

> db.c5.find()

{ "_id" : 1, "ary" : [ ] }

> db.c5.update({_id:1},{$push:{ary:{$each:[1,2,3],$slice:-2}}})

> db.c5.find()

{ "_id" : 1, "ary" : [ 2, 3 ] }

$sort

语法:db.collection.update(<query>,

                      { $push:{

                                 <field>: {

                                           $each: [ <document1>,

                                                    <document2>,

                                                    ...

                                                  ],

                                           $slice: <num>,

                                           $sort: <sort document>,

                                          }

                              }

                      }

                    )

$sort将数组中的元素按照一定的规则进行排序,同样1表示正序,-1表示倒序。

$sort必须与$push、$each结合使用,并且$each值的数组中的元素都必须为对象。

> db.c5.find()

{ "_id" : 1, "ary" : [ ] }

>db.c5.update({_id:1},{$push:{ary:{$each:[{_id:1,score:3},{_id:2,score:5},{_id:3,score:1}],$slice:-5,$sort:{score:-1}}}})

> db.c5.find()

{ "_id" : 1, "ary" : [ { "_id" : 2,"score" : 5 }, {"_id" : 1, "score" : 3 }, {"_id" : 3, "score" : 1 } ] }

MongoDB自学笔记12---4.4 更新文档

上一篇:分享 - Hybrid 开发将博客园集成到自己的网站中 - 效果高大上 :)


下一篇:LinuxMint 19.3升级至LinuxMint 20