简体   繁体   English

MeteorJS:更新数组内部的对象

[英]MeteorJS : UPDATE AN OBJECT inside array

I'm trying to update the comment_delete = 'false' to 'true' on the second object within an array. 我试图将数组中第二个对象上的comment_delete ='false'更新为'true' Help Please .... 请帮助 ....

 "_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" } ], } 

Try this query: 试试这个查询:

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

I dont have an idea about your match criteria since you didnt mention it in the question. 由于您在问题中没有提及您,因此我对您的比赛条件一无所知。 Change them as per your requirements. 根据您的要求更改它们。 This change comment_delete to true as per the comment_id mentioned. 这种变化comment_deletetrue为每comment_id提及。

Output is: 输出为:

{
"_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}})
  1. 试试上面提到的查询工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM