[英]How to Update An Array of Subdocuments on a MongoDB Collection in MeteorJS
[英]MeteorJS : UPDATE AN OBJECT inside array
我试图将数组中第二个对象上的comment_delete ='false'更新为'true' 。 请帮助 ....
"_id" : "jLkRdxocZzheefWF3", "comments" : [ { "comment_id" : "\624334", "comment" : " test", "user" : "peter pan", "userId" : "MQtp4i8bZeLYSLbr5", "comment_delete" : "false" }, { "comment_id" : "\973101", "comment" : " add", "user" : "peter pan", "userId" : "MQtp4i8bZeLYSLbr5", "comment_delete" : "false" } ], }
试试这个查询:
db.collection.update(
{_id:"jLkRdxocZzheefWF3"}, //add your first match criteria here, keep '{}' if no filter needed.
{$set:{"comments.$[element].comment_delete":"true"}},
{arrayFilters:[{"element.comment_id":"\u0007973101"}]}
)
由于您在问题中没有提及您,因此我对您的比赛条件一无所知。 根据您的要求更改它们。 这种变化comment_delete
到true
为每comment_id
提及。
输出为:
{
"_id" : "jLkRdxocZzheefWF3",
"comments" : [
{
"comment_id" : "\u0003624334",
"comment" : " test",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "false"
},
{
"comment_id" : "\u0007973101",
"comment" : " add",
"user" : "peter pan",
"userId" : "MQtp4i8bZeLYSLbr5",
"comment_delete" : "true"
}
]
}
db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.