This question already has an answer here:
I have objects like this
{
"_id" : ObjectId("5742be02289512cf98bf63e3"),
"name" : "test1",
"name" : "test1",
"attributes" : [
{
"name" : "x",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e8")
},
{
"name" : "y",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e7")
},
{
"name" : "z",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e6")
}
],
"__v" : 6
}
And I want to update all documents, and set for each attribute new field. So I want to run single query, to update all documents at once. I think, this query will do it
db.spaces.update({}, { $set: { "attributes.0.weight": 2 } }, {multi: true})
But when I run this query, I get an error.
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: attributes.$.weight"
So I cant understand why. Please help
The positional operator needs a match, from the match part of your update query.
for example:
here the first parameter for update operation will match the array
attributes
where any element has a propertyname=="x"
, for any eleent that matches the condition the position operator can be used to update it.So, because
name='x'
, in this case the first matching element would be,and it will get updated.
Now from your question I understand you want to update the document in a way that in each document your first element,
attribute
gets a new value forweight=2
.you can do something like
What we do here is match all element in array attribute. and the we use the positional operator to update the first element of that array
You need to include the array field as part of the query document in order to use the
positional operator
.For example, if you want to update the first array element i.e. with
{ "attributes.name": "x" }
then you could follow the pattern:For the newer MongoDB versions
3.2.X
, you could use theupdateMany()
method to update multiple documents within the collection based on the filter above.