在官方的mongoose网站上,我发现如何通过数组中的_id删除嵌入式文档:
post.comments.id(my_id).remove();
post.save(function (err) {
// embedded comment with id `my_id` removed!
});
Run Code Online (Sandbox Code Playgroud)
我很感兴趣如何更新而不是删除这个?
gui*_*mie 14
它看起来像这样:
YOURSCHEMA.update(
{ _id: "DocumentObjectid" , "ArrayName.id":"ArrayElementId" },
{ $set:{ "ArrayName.$.TheParameter":"newValue" } },
{ upsert: true },
function(err){
}
);
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我正在搜索一个带有id参数的元素,但它可能是objectId类型的实际_id参数.
另请参阅:MongooseJS Doc - 更新集和类似的SO问题
Cri*_*s-O 11
你可以做到
var comment = post.comments.id(my_id);
comment.author = 'Bruce Wayne';
post.save(function (err) {
// emmbeded comment with author updated
});
Run Code Online (Sandbox Code Playgroud)