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
source share