Javascript MeteorJS:更新数组中的对象

Javascript MeteorJS:更新数组中的对象,javascript,mongodb,meteor,collections,Javascript,Mongodb,Meteor,Collections,我正在尝试将数组中第二个对象的注释\u delete='false'更新为'true'。请帮忙 “_id”:“jLkRdxocZzheefWF3”, “评论”:[ { “注释id”:“\u0003624334”, “评论”:“测试”, “用户”:“彼得·潘”, “userId”:“MQtp4i8bZeLYSLbr5”, “注释_删除”:“错误” }, { “注释id”:“\u0007973101”, “评论”:“添加”, “用户”:“彼得·潘”, “userId”:“MQtp4i8bZeLY

我正在尝试将数组中第二个对象的注释\u delete='false'更新为'true'。请帮忙


“_id”:“jLkRdxocZzheefWF3”,
“评论”:[
{
“注释id”:“\u0003624334”,
“评论”:“测试”,
“用户”:“彼得·潘”,
“userId”:“MQtp4i8bZeLYSLbr5”,
“注释_删除”:“错误”
},
{
“注释id”:“\u0007973101”,
“评论”:“添加”,
“用户”:“彼得·潘”,
“userId”:“MQtp4i8bZeLYSLbr5”,
“注释_删除”:“错误”
}
],
}
尝试此查询:

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"}]}
)
我不知道你的比赛标准,因为你在问题中没有提到。根据您的要求进行更改。根据前面提到的
注释id
注释删除
更改为
true

输出为:

{
"_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"
    }
 ]
}
  • 试试上面提到的查询,它会工作的

  • comments[1]。comment\u delete=true
    可能与Thank Rahul重复,但我更新时仍有一些错误,更新失败:MongoError:无法使用部分(comments of comments.$[test]。comment\u delete)遍历元素({comments:[{comment\u id:“\u0007973101”……您需要有mongo 3.6版。查询应该从mongo shell执行(以防您使用第三方工具)。上述查询已经过测试,并在我的本地工作。其次,我不会根据您的要求进行回答(您取消选择我的答案以获得我的即时答复)。
    db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})