【问题标题】:MeteorJS : UPDATE AN OBJECT inside arrayMeteorJS:更新数组内的对象
【发布时间】:2018-09-27 13:36:26
【问题描述】:

我正在尝试将数组中第二个对象的 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


【解决方案1】:

试试这个查询:

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_id,将comment_delete 更改为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"
    }
 ]
}

【讨论】:

  • 感谢 Rahul ,但我仍然有一些错误更新,更新失败:MongoError: cannot use the part (cmets of cmets.$[test].comment_delete) to traverse the element ({cmets: [ {comment_id:“\u0007973101”......
  • 你需要有 mongo 3.6 版。并且应该从 mongo shell 执行查询(以防万一您使用第三方工具)。上面的查询已经过测试,并且可以在我的本地运行。其次,我不会根据您的要求来回答(您已取消选择我的答案以获得我的即时回复)。
【解决方案2】:
db.users.update({'_id':'jLkRdxocZzheefWF3',"comments.comment_id":"\u0007973101"},{$set:{'comments.$.comment_delete':true}})
  1. 试试上面提到的查询就可以了

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 2018-06-30
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2019-05-25
    相关资源
    最近更新 更多