Mongoose:更新嵌套的文档数组

我的collections如下:

"_id" : ObjectId("5751f7892ae95d601f40411d"), "doc" : [ { "org" : ObjectId("5751f7892ae95d601f40411c"), "action" : 0, "_id" : ObjectId("5751f7892ae95d601f40411e") }, { "org" : ObjectId("5751952cace204c507fad255"), "action" : 1, "_id" : ObjectId("575217ce341cf6512b8dff39") } ] 

我想用org:5751952cace204c507fad255更新文档中的操作字段,所以action将等于2我知道这已经被多次回答,但它不适用于我

这是我尝试过但收集没有改变:

  Model.update( { "_id":ObjectId("5751f7892ae95d601f40411d"), "doc.org":ObjectId("5751952cace204c507fad255") }, { "$inc": { "doc.$.action": 1 } } ) 

可以尝试使用位置操作符$来增加匹配的操作doc.$.action

喜欢:

 //assume you passed modelId and orgId in request body // According to your tag you may used mongoose so use mongoose.Types.ObjectId('5751f7892ae95d601f40411d') instead of ObjectId("5751f7892ae95d601f40411d") // or direct req.body.modelId without convert Model.update( { "_id": req.body.modelId, "doc.org": req.body.orgId }, { "$inc": { "doc.$.action": 1 } }, function(error, updatedData) { if(error) { return res.status(400).send(error); } return res.status(200).send(updatedData); } );