簡體   English   中英

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_deletetrue為每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}})
  1. 試試上面提到的查詢工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM