Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update value of a key in a list of a json in Mongo

I have a document---

   Employees:[
       {
          name:"abc",
          contact:"123",
          email:"[email protected]"
       },
       {
          name:"efg",
          contact:"456",
          email:"[email protected]"
       },
       {
          name:"hij",
          contact:"789",
          email:"[email protected]"
       }
  ]

I need to update name with value= "abc" for all the keys name in the list.

I have tried to update like

db.collection.update(
    { "_id" : ObjectId("5308595e3256e758757b4d2f") }, 
    { "$set": { "Employees.name " : "abc" } } 
);

But getting the error: cannot use the part (Employees of Employees.name) to traverse the element.

like image 369
Shalu Avatar asked May 23 '14 05:05

Shalu


People also ask

How do I update a list in MongoDB?

You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

How do you update data in an array of objects 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.

What does update keyword do in MongoDB?

The update() method updates the values in the existing document in the collections of MongoDB. When you update your document the value of the _id field remains unchanged.


1 Answers

These are in an array, so that is why your current statement does not work. You have a few options to do this as there is no simple statement to do this.

1. You know how many elements are in the array, so set them explicitly with "dot-notation"

    db.collection.update(
        { "_id" : ObjectId("5308595e3256e758757b4d2f") }, 
        { 
            "$set": { 
                "Employees.0.name " : "abc",
                "Employees.1.name " : "abc",
                "Employees.2.name " : "abc"
            }
        } 
    );

2. You don't know but are prepared to issue this update until the returned "modified" documents becomes 0. Then you can use a positional $ operator in the update but this will only ever match one element at a time:

     db.collection.update(
        { 
            "_id" : ObjectId("5308595e3256e758757b4d2f"),
            "Employees.name": { "$ne": "abc" }
        }, 
        { 
            "$set": { 
                "Employees.$.name " : "abc"
            }
        } 
    );

3. Retrieve the document and update all the array members in code:

    var doc = db.collection.findOne({ 
        "_id": ObjectId("5308595e3256e758757b4d2f") 
    });

    doc.Employee.forEach(function(emp) {
        emp.name = "abc";
    });
    db.collection.update(
       { "_id": doc._id },
       { "$set": { "Employee": doc.Employeee } }
    )

Those are the basic methods and doing this, along with some practical example of why this cannot be presently done in a single statement just updating every array member field.

like image 169
Neil Lunn Avatar answered Oct 16 '22 06:10

Neil Lunn