mongoose。 更新数组中的embedded式文档
在一个正式的mongoose网站,我发现如何删除embedded的文件_id在数组中:
post.comments.id(my_id).remove(); post.save(function (err) { // embedded comment with id `my_id` removed! });
我很感兴趣,我怎样才能更新,而不是删除这个?
你可以做
var comment = post.comments.id(my_id); comment.author = 'Bruce Wayne'; post.save(function (err) { // emmbeded comment with author updated });
它看起来像这样:
YOURSCHEMA.update( { _id: "DocumentObjectid" , "ArrayName.id":"ArrayElementId" }, { $set:{ "ArrayName.$.TheParameter":"newValue" } }, { upsert: true }, function(err){ } );
在这个例子中,我正在使用一个id参数来search一个元素,但它可能是objectIdtypes的实际_id参数。
另请参阅: MongooseJS文档 – 更新集和类似SO问题