我希望能够: 查找具有特定ID的文档,然后根据shortID在旧数组中查找文档,并更新与shortID匹配的嵌入文档的sets数组。
显然,有些东西还没有为我点击过。
我有以下结构:
{
"id" : "5706e5b5cbc61e5bf3a9f4e7",
"legacy" : [
{
"shortID" : "B1zHAllg",
"timeStamp" : "Apr 16th 2016",
"sets" : [
{
"weight" : "7",
"reps" : "7"
},
{
"weight" : "7",
"reps" : "7"
}
]
},
{
"shortID" : "HyUVCegx",
"timeStamp" : "Apr 16th 2016",
"sets" : [
{
"weight" : "6",
"reps" : "6"
},
{
"weight" : "6",
"reps" : "6"
}
]
}
]
}
我尝试了很多选项,但觉得我最接近尝试以下内容:
db.bench.findAndModify({query:{id:"5706e5b5cbc61e5bf3a9f4e7"},sort:{legacy:{$elemMatch:{shortID:"HyUVCegx"}}},update:{$set:{sets:[9]}}})
db.bench.update({id : "5706e5b5cbc61e5bf3a9f4e7", legacy:{$elemMatch:{shortID:"HyUVCegx"}}}, {$set : { sets: [{"weight":"5", "reps" :"10"}] }})
答案 0 :(得分:1)
您希望对此类操作使用位置更新。请参阅positional updates
的文档示例:在MongoDB shell中,您将执行以下操作:
db.collection.updateOne(
{"legacy.shortID":"HyUVCegx"},
{$set:
{"legacy.$.sets":
[{"weight":"15", "reps" :"10"}]
}
})
上述查询将修改legacy
匹配条件中shortID = "HyUVCegx"