向已有的数组末尾加入一个元素
初始
{
"_id" : 1.0,
"arr" : [
5
]
}
db.getCollection('user').update(
{"_id": 1.0},
{$push: {"arr": 10}}
)
结果
{
"_id" : 1.0,
"arr" : [
5,
10.0
]
}
使用$position添加到任意位置
// 在arr数组第二项插入四项数据
db.getCollection('user').update(
{"_id": 1.0},
{$push: {"arr": {$each: [15,16,17,18], $position: 1}}}
)
结果
{
"_id" : 1.0,
"arr" : [
5,
15.0,
16.0,
17.0,
18.0,
10.0
]
}