Error positional operator mongodb

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 the documents and set a new field for each attribute. Therefore, I want to run one request in order 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 request, I get an error.

"code": 16837,
"errmsg": "The positional operator did not find a match from the request. Unexpanded update: attributes. $. Weight"

So I can’t understand why. Please, help

+9
source share
2 answers

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 element of an array, i.e. using { "attributes.name": "x" } , you can follow the pattern:

 db.spaces.update( { "attributes.name": "x" }, // <-- the array field must appear as part of the query document. { "$set": { "attributes.$.weight": 2 } }, { "multi": true } ) 

For newer versions of MongoDB 3.2.X you can use the updateMany() method to update multiple documents in the collection based on the filter above.

+18
source

The positional operator needs a match from the matching part of your update request.

eg:

 db.spaces.update({ "attributes.name": "x" }, { $set: { "attributes.0.weight": 2 } }, {multi: true}) 

here the first parameter for the update operation will correspond to the attributes array, where any element has name=="x" property name=="x" , for any element that matches the condition, the position operator can be used to update it.

So, since name='x' , in this case the first matching element will be

 { "name" : "x", "color" : "0xd79c9c", "_id" : ObjectId("5742be02289512cf98bf63e8") }, 

and it will be updated.

Now, based on your question, I understand that you want to update the document so that in each document your first attribute element gets a new value for weight=2 .

you can do something like

 db.spaces.update({ "attributes.name": { $regex: /^(?=[\S\s]{10,8000})[\S\s]*$/ } }, { $set: { "attributes.0.weight": 2 } }, {multi: true}) 

What we are doing here is matching all the elements in an array attribute. and we use the positional operator to update the first element of this array

+3
source

All Articles