Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update an item in an array that is in an array

Tags:

mongodb

I have a document in a mongodb collection like this :

{
    sessions : [
        {
            issues : [ 
                {
                    id : "6e184c73-2926-46e9-a6fd-357b55986a28",
                    text : "some text"
                },   
                {
                    id : "588f4547-3169-4c39-ab94-8c77a02a1774",
                    text : "other text"
                }
            ]
        } 
    ]
} 

And I want to update the issue with the id 588f4547-3169-4c39-ab94-8c77a02a1774 in the first session.

The problem is that I only know that it's the first session and the issue id (NOT the index of the issue !)

So I try something like this :

db.mycollection.update({ "sessions.0.issues.id" : "588f4547-3169-4c39-ab94-8c77a02a1774"}, 
                       { $set: { "sessions.0.issues.$.text" : "a new text" }})

But I got the following result :

WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: sessions.0.issues.$.text"
    }

How can I do this ?

Thanks for help.

like image 722
Frédéric Guégan Avatar asked Jun 11 '14 15:06

Frédéric Guégan


People also ask

How do you update one object in an array of objects?

To update an object's property in an array of objects, use the map() method to iterate over the array. On each iteration, check if the current object is the one to be updated. If it is, modify the object and return the result, otherwise return the object as is. Copied!

How do you update an element in an array?

To update all the elements of an array, call the forEach() method on the array, passing it a function. The function gets called for each element in the array and allows us to update the array's values.

Can we change the value of an element in an array?

To change the value of an object in an array:Call the findIndex() method to get the index of the specific object. Access the array at the index and change the property's value using dot notation. The value of the object in the array will get updated in place.

How do you update an object in an array in React?

To update an object in an array in React state: Use the map() method to iterate over the array. On each iteration, check if a certain condition is met. Update the object that satisfies the condition and return all other objects as is.


1 Answers

You have to use this (apparently equivalent) query:

db.mycollection.update({"sessions.0.issues": {$elemMatch: {id: <yourValue>}}}, {$set: {"sessions.0.issues.$.text": "newText"}})

Notice that your update expression was correct.

More information about $elemMatch.

Btw, MongoDB reference explicits that $ operator does not work "with queries that traverse nested arrays".

Important: $elemMatch only works with version 4 or more.

like image 195
dgiugg Avatar answered Oct 24 '22 02:10

dgiugg