mongodb入门-12更新2
mongodb入门-12更新2
mongodb入门-11更新1
http://www.zzzyk.com/database/201305/213137.html
继续介绍mongoDB中的更新.
$inc
为一个字段添加加上一个值,这个方法只能对数字操作,也就是说只能给数字加上一个值,当然这个值可以是负数.
[html]
> 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}}) <span style="font-family: Arial, Helvetica, sans-serif;">-->将name为user6的年龄age加上-4,也就是减去4</span>
> 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
当文档中包含该字段的时候,更新该字段,如果该文档中没有该字段,则为本文档添加一个字段.
[html]
> 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:{易做图:"nan"}}) -->将name为user2的易做图设置为nan,但是没有该字段,所以为该文档添加易做图字段并赋值为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", "易做图" : "nan" }
$unset
删除文档中的一个字段.
[html]
> 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", "易做图" : "nan" }
> db.user.update({name:"user2"},{$unset:{易做图:1}}) -->删除name为user2的易做图字段.如果删除一个不存在的字段,并不会报错,没有什么效果
> 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" }
$push
将一个数字存入一个数组,分为三种情况,如果该字段存在,则直接将数字存入数组.如果该字段不存在,创建字段并且将数字插入该数组.如果更新的字段不是数组,会报错的.
[html]
> 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一次只会存入一个字段,代码如下:
[html]
> 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
将多个数值一次存入数组.
[html]
> 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 ] }
$addToSe