MeteorJS:更新数组内部的对象

时间:2018-04-17 14:19:10

标签: javascript mongodb meteor collections

我正在尝试将 comment_delete ='false'更新为数组中第二个对象的'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" : "false"
		}
	],
	
}

2 个答案:

答案 0 :(得分:0)

尝试此查询:

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,此更改为truecomment_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"
    }
 ]
}

答案 1 :(得分:0)

db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})
  1. 尝试上面提到的查询