)
将一个数字存入一个数组,分为三种情况,如果该字段存在,则直接将数字存入数组.如果该字段不存在,创建字段并且将数字插入该数组.如果更新的字段不是数组,会报错的.
> 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设置为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:
删除数组中的一个元素,如果删除的字段不是数组,会报错
> 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(
{
$addToSet: {
}
)
结合$push:
db.collection.update(
{
$push:{
}
)
结合$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(
{ $push: {
$each: [
$slice:
}
}
}
)
$slice需要和$push结合使用,截取一定长度的数组