How to update array of subdocuments in MongoDB?

To update an array of subdocuments in MongoDB, use the update() method with the $set operator and the $ positional operator to target specific array elements.

Syntax

db.collection.update(
    {
        "field": "value",
        "arrayField.subField": "matchValue"
    },
    {
        $set: { "arrayField.$.subField": "newValue" }
    }
);

Sample Data

db.demo134.insertMany([
    {
        "EmployeeId": 101,
        "EmployeeDetails": [
            {"EmployeeName": "Chris", "EmployeeAge": 27},
            {"EmployeeName": "Bob", "EmployeeAge": 28}
        ]
    },
    {
        "EmployeeId": 102,
        "EmployeeDetails": [
            {"EmployeeName": "David", "EmployeeAge": 24},
            {"EmployeeName": "Carol", "EmployeeAge": 29}
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e319b2f68e7f832db1a7f7c"),
        ObjectId("5e319b4468e7f832db1a7f7d")
    ]
}

Display Current Data

db.demo134.find().pretty();
{
    "_id": ObjectId("5e319b2f68e7f832db1a7f7c"),
    "EmployeeId": 101,
    "EmployeeDetails": [
        {
            "EmployeeName": "Chris",
            "EmployeeAge": 27
        },
        {
            "EmployeeName": "Bob",
            "EmployeeAge": 28
        }
    ]
}
{
    "_id": ObjectId("5e319b4468e7f832db1a7f7d"),
    "EmployeeId": 102,
    "EmployeeDetails": [
        {
            "EmployeeName": "David",
            "EmployeeAge": 24
        },
        {
            "EmployeeName": "Carol",
            "EmployeeAge": 29
        }
    ]
}

Example: Update Subdocument Field

Update Chris's name to Robert in EmployeeId 101 ?

db.demo134.update(
    {
        "EmployeeId": 101,
        "EmployeeDetails.EmployeeName": "Chris"
    },
    {
        $set: {
            "EmployeeDetails.$.EmployeeName": "Robert"
        }
    }
);
WriteResult({ "nMatched": 1, "nUpserted": 0, "nModified": 1 })

Verify Result

db.demo134.find().pretty();
{
    "_id": ObjectId("5e319b2f68e7f832db1a7f7c"),
    "EmployeeId": 101,
    "EmployeeDetails": [
        {
            "EmployeeName": "Robert",
            "EmployeeAge": 27
        },
        {
            "EmployeeName": "Bob",
            "EmployeeAge": 28
        }
    ]
}
{
    "_id": ObjectId("5e319b4468e7f832db1a7f7d"),
    "EmployeeId": 102,
    "EmployeeDetails": [
        {
            "EmployeeName": "David",
            "EmployeeAge": 24
        },
        {
            "EmployeeName": "Carol",
            "EmployeeAge": 29
        }
    ]
}

Key Points

  • The $ positional operator identifies the first matching array element
  • Query must include both parent document field and array element field for proper matching
  • Only the first matching subdocument is updated when using $

Conclusion

Use the $ positional operator with $set to update specific fields in subdocuments. The query condition must match both the parent document and the target array element to ensure accurate updates.

Updated on: 2026-03-15T02:13:11+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements