25

I have a collection with objects like this

{
    "_id" : ObjectId("5742be02289512cf98bf63e3"),
    "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 a single query to update all documents at once. I think, this query will do

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"

I can't understand why. Please help

TOPKAT
  • 6,667
  • 2
  • 44
  • 72
Gor
  • 2,808
  • 6
  • 25
  • 46

2 Answers2

42

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:

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

For the newer MongoDB versions 3.2.X, you could use the updateMany() method to update multiple documents within the collection based on the filter above.

chridam
  • 100,957
  • 23
  • 236
  • 235
  • 7
    db.spaces.update({"attributes.name": { $exists: true }}, { $set: {"attributes.$.weight": 3}}, {multi: true}) this query updates only first item in array – Gor Jun 03 '16 at 11:24
  • I need to update all elements in array for all documents – Gor Jun 03 '16 at 11:25
  • @Gor Oh I see, let me update the answer – chridam Jun 03 '16 at 11:26
  • @Gor Actually a duplicate exists which offers the solution – chridam Jun 03 '16 at 11:28
  • I cant understand, why { $exists: true} dont work? How I understand, it will match all elements in array – Gor Jun 03 '16 at 11:29
  • 3
    @Gor The reason why it won't work in this case is that positional `$` operator acts as a placeholder for the **first** element that matches the query document, and not all the elements in the array. Consult the answers in the marked duplicate for a solution. – chridam Jun 03 '16 at 11:31
  • please give me link for duplicate, thank you – Gor Jun 03 '16 at 11:39
  • @Gor It's right there at the top of your question. In case you don't see it :) [**How to Update Multiple Array Elements in mongodb**](http://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – chridam Jun 03 '16 at 11:40
10

The positional operator needs a match, from the match part of your update query.

for example:

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

here the first parameter for update operation will match the array attributes where any element has a property name=="x", for any element 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,

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

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 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 do here is match all element in array attribute. and we use the positional operator to update the first element of that array

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94
Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88