简体   繁体   中英

MeteorJS : UPDATE AN OBJECT inside array

I'm trying to update the comment_delete = 'false' to 'true' on the second object within an array. 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.

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. 试试上面提到的查询工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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