How to remove an element from a doubly-nested array in a MongoDB document?

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator with proper dot notation to target the nested array location.

Syntax

db.collection.update(
    { "queryCondition": "value" },
    { $pull: { "outerArray.index.innerArray": { "field": "valueToRemove" } } }
);

Sample Data

To understand the concept, let us create a collection with sample documents ?

db.removeElementFromDoublyNestedArrayDemo.insertMany([
    {
        "_id": "1",
        "UserName": "Larry",
        "UserDetails": [
            {
                "UserCountryName": "US",
                "UserLocation": [
                    {
                        "UserCityName": "New York"
                    },
                    {
                        "UserZipCode": "10001"
                    }
                ]
            }
        ]
    },
    {
        "_id": "2",
        "UserName": "Mike",
        "UserDetails": [
            {
                "UserCountryName": "UK",
                "UserLocation": [
                    {
                        "UserCityName": "Bangor"
                    },
                    {
                        "UserZipCode": "20010"
                    }
                ]
            }
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": {
        "0": "1",
        "1": "2"
    }
}

Display all documents from the collection ?

db.removeElementFromDoublyNestedArrayDemo.find().pretty();
{
    "_id": "1",
    "UserName": "Larry",
    "UserDetails": [
        {
            "UserCountryName": "US",
            "UserLocation": [
                {
                    "UserCityName": "New York"
                },
                {
                    "UserZipCode": "10001"
                }
            ]
        }
    ]
}
{
    "_id": "2",
    "UserName": "Mike",
    "UserDetails": [
        {
            "UserCountryName": "UK",
            "UserLocation": [
                {
                    "UserCityName": "Bangor"
                },
                {
                    "UserZipCode": "20010"
                }
            ]
        }
    ]
}

Example: Remove Element from Nested Array

Here is the query to remove the UserZipCode element from Mike's UserLocation array ?

db.removeElementFromDoublyNestedArrayDemo.update(
    { _id: "2" },
    { $pull: { "UserDetails.0.UserLocation": { "UserZipCode": "20010" } } }
);
WriteResult({ "nMatched": 1, "nUpserted": 0, "nModified": 1 })

Verify Result

Let us check the documents to verify the removal ?

db.removeElementFromDoublyNestedArrayDemo.find().pretty();
{
    "_id": "1",
    "UserName": "Larry",
    "UserDetails": [
        {
            "UserCountryName": "US",
            "UserLocation": [
                {
                    "UserCityName": "New York"
                },
                {
                    "UserZipCode": "10001"
                }
            ]
        }
    ]
}
{
    "_id": "2",
    "UserName": "Mike",
    "UserDetails": [
        {
            "UserCountryName": "UK",
            "UserLocation": [
                {
                    "UserCityName": "Bangor"
                }
            ]
        }
    ]
}

Key Points

  • Use $pull with dot notation: outerArray.index.innerArray
  • Specify the exact field−value pair to match and remove from the nested array
  • The index 0 refers to the first element in the outer UserDetails array

Conclusion

The $pull operator effectively removes elements from doubly−nested arrays using dot notation. Combine array position indexing with field matching to precisely target nested array elements for removal.

Updated on: 2026-03-15T00:05:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements