更新数组中的embedded式文档 – Mongodb + Node Driver

我希望能够:查找具有特定id的文档,然后基于shortID在legacy数组中find文档,并更新与shortID匹配的embedded文档的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" } ] } ] } 

我已经尝试了很多select,但是觉得我是最接近的尝试以下方法:

 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"}] }}) 

你会想要使用这种操作的位置更新。 查看位置更新的文档

例如:在MongoDB shell中,您将按以下方式执行:

 db.collection.updateOne( {"legacy.shortID":"HyUVCegx"}, {$set: {"legacy.$.sets": [{"weight":"15", "reps" :"10"}] } }) 

上面的查询将修改legacy匹配条件中的文档内容,其中shortID = "HyUVCegx"