继续介绍mongoDB中的更新.
$inc
为一个字段添加加上一个值,这个方法只能对数字操作,也就是说只能给数字加上一个值,当然这个值可以是负数.
> 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.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.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" }
将一个数字存入一个数组,分为三种情况,如果该字段存在,则直接将数字存入数组.如果该字段不存在,创建字段并且将数字插入该数组.如果更新的字段不是数组,会报错的.
> 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.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
与$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.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.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
删除数组中的多个值,跟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" }
$rename
为字段改名
> 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" }