如果我在MongoDb doc中有以下数组:
"example": {
[
"number": 5,
"someValue": "V"
],
[
"number": 7,
"someValue": "H"
]
}
如何将下面的数组添加到上面的数组顶部:
[
"number": 3,
"someValue": "S"
]
使原始数组变为:
"example": {
[
"number": 3,
"someValue": "S"
],
[
"number": 5,
"someValue": "V"
],
[
"number": 7,
"someValue": "H"
]
}
答案 0 :(得分:2)
您可以使用 $push
运算符的选项实现此目的:
db.collection.update({},
{
$push:{
"arr":{
$each:[
{
"number": 3,
"someValue": "S"
}
],
$position: 0
}
}
})
$position
指定元素的插入位置。