How to update value of a key in a list of a json in MongoDB?

To update a value of a key in a list (array) of JSON documents in MongoDB, you can use the $ positional operator for single element updates or retrieve the document, modify it in JavaScript, and replace the entire array using $set.

Syntax

// Method 1: Update single array element
db.collection.update(
    {"arrayField.key": "matchValue"},
    { $set: { "arrayField.$.key": "newValue" } }
);

// Method 2: Update multiple elements using JavaScript
var doc = db.collection.findOne({"_id": ObjectId("id")});
doc.arrayField.forEach(function(element) {
    element.key = "newValue";
});
db.collection.update(
    {"_id": doc._id},
    { $set: { "arrayField": doc.arrayField } }
);

Sample Data

db.updateListOfKeyValuesDemo.insertOne({
    "StudentDetails": [
        {
            "StudentName": "John",
            "StudentAge": 23,
            "StudentCountryName": "US"
        },
        {
            "StudentName": "Carol",
            "StudentAge": 24,
            "StudentCountryName": "UK"
        },
        {
            "StudentName": "Bob",
            "StudentAge": 22,
            "StudentCountryName": "AUS"
        }
    ]
});
{
    "acknowledged": true,
    "insertedId": ObjectId("5c9b5b759882024390176545")
}

View Sample Data

db.updateListOfKeyValuesDemo.find().pretty();
{
    "_id": ObjectId("5c9b5b759882024390176545"),
    "StudentDetails": [
        {
            "StudentName": "John",
            "StudentAge": 23,
            "StudentCountryName": "US"
        },
        {
            "StudentName": "Carol",
            "StudentAge": 24,
            "StudentCountryName": "UK"
        },
        {
            "StudentName": "Bob",
            "StudentAge": 22,
            "StudentCountryName": "AUS"
        }
    ]
}

Method 1: Update Single Element Using $ Operator

Update only John's name to "Ramit" ?

db.updateListOfKeyValuesDemo.update(
    {"StudentDetails.StudentName": "John"},
    { $set: { "StudentDetails.$.StudentName": "Ramit" } }
);

Method 2: Update All Elements Using JavaScript

Update all StudentName values to "Ramit" in the array ?

var documentFromCollection = db.updateListOfKeyValuesDemo.findOne({
    "_id": ObjectId("5c9b5b759882024390176545")
});

documentFromCollection.StudentDetails.forEach(function(updateStudent) {
    updateStudent.StudentName = "Ramit";
});

db.updateListOfKeyValuesDemo.update(
    { "_id": documentFromCollection._id },
    { "$set": { "StudentDetails": documentFromCollection.StudentDetails } }
);
WriteResult({ "nMatched": 1, "nUpserted": 0, "nModified": 1 })

Verify Result

db.updateListOfKeyValuesDemo.find().pretty();
{
    "_id": ObjectId("5c9b5b759882024390176545"),
    "StudentDetails": [
        {
            "StudentName": "Ramit",
            "StudentAge": 23,
            "StudentCountryName": "US"
        },
        {
            "StudentName": "Ramit",
            "StudentAge": 24,
            "StudentCountryName": "UK"
        },
        {
            "StudentName": "Ramit",
            "StudentAge": 22,
            "StudentCountryName": "AUS"
        }
    ]
}

Key Points

  • Use $ operator for updating a single matching array element
  • Use JavaScript forEach() method to update multiple array elements at once
  • The $set operator replaces the entire array when using Method 2

Conclusion

MongoDB provides flexible ways to update array elements: use the $ positional operator for single elements or JavaScript iteration with $set for bulk updates. Choose based on whether you need to update one or multiple array elements.

Updated on: 2026-03-15T00:32:01+05:30

823 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements