如何更新MongoDB中的多个数组元素 [英] How to Update Multiple Array Elements in mongodb
问题描述
我有持有元素的数组一个蒙戈文件。
I have a Mongo document which holds an array of elements.
我想重置阵列中的所有对象的 .handled
属性其中 .profile文件
= XX 。
该文件是在以下形式:
I'd like to reset the .handled
attribute of all objects in the array where .profile
= XX.
The document is in the following form:
{
"_id" : ObjectId("4d2d8deff4e6c1d71fc29a07"),
"user_id" : "714638ba-2e08-2168-2b99-00002f3d43c0",
"events" : [
{
"handled" : 1,
"profile" : 10,
"data" : "....."
}
{
"handled" : 1,
"profile" : 10,
"data" : "....."
}
{
"handled" : 1,
"profile" : 20,
"data" : "....."
}
...
]
}
所以,我尝试了以下内容:
so, I tried the following:
.update({"events.profile":10},{$set:{"events.$.handled":0}},false,true)
但它仅更新的第一匹配的阵列中的每个文件中的元素。 (这对 $定义的行为 - 的位置运营商)
however it updates only the first matched array element in each document. (That's the defined behavior for $ - the positional operator.)
我如何更新全部匹配的数组元素?
How can I update all matched array elements?
推荐答案
目前时刻,不可能使用该位置操作者来更新数组中的所有项目。见JIRA http://jira.mongodb.org/browse/SERVER-1243
At this moment it is not possible to use the positional operator to update all items in an array. See JIRA http://jira.mongodb.org/browse/SERVER-1243
作为一个工作你周围可以:
As a work around you can:
- 单独更新每个项目
(events.0.handled events.1.handled
...)或者... - 阅读文档,做编辑
手动并保存它取代
旧的(看如果更新
当前,如果你想确保的
原子更新)
这篇关于如何更新MongoDB中的多个数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!