MeteorJS:更新数组内部的对象

问题描述 投票:0回答:2

我正在尝试将数组中第二个对象的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"
		}
	],
	
}
javascript mongodb meteor collections
2个回答
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,这将true改为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"
    }
 ]
}

0
投票
db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})
  1. 尝试上面提到的查询它是否有效
© www.soinside.com 2019 - 2024. All rights reserved.