Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb error: The positional operator did not find the match needed from the query [duplicate]

Tags:

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

like image 446
Gor Avatar asked Jun 03 '16 10:06

Gor


People also ask

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

How do I remove an element from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

How do you update an array object in MongoDB?

Update Documents in an ArrayThe positional $ operator facilitates updates to arrays that contain embedded documents. Use the positional $ operator to access the fields in the embedded documents with the dot notation on the $ operator.


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 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.

like image 122
chridam Avatar answered Sep 19 '22 13:09

chridam


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

like image 44
Naeem Shaikh Avatar answered Sep 17 '22 13:09

Naeem Shaikh