Search for a text in MongoDBs Double Nested Array?

To search for a text in MongoDB's double nested array, use dot notation to traverse through multiple array levels. The dot notation format is parentArray.childArray.fieldName to target specific fields deep within nested structures.

Syntax

db.collection.find({
    "parentArray.childArray.fieldName": "searchValue"
});

Sample Data

Let us create a collection with double nested array documents ?

db.doubleNestedArrayDemo.insertMany([
    {
        "StudentId": "1000",
        "StudentName": "Larry",
        "StudentDetails": [
            {
                "ProjectName": "Online Banking",
                "ProjectDetails": [
                    {
                        "TechnologyUsed": "Java"
                    },
                    {
                        "TechnologyUsed": "MySQL in Backend"
                    }
                ]
            }
        ]
    },
    {
        "StudentId": "1001",
        "StudentName": "Robert",
        "StudentDetails": [
            {
                "ProjectName": "Student Web Tracker",
                "ProjectDetails": [
                    {
                        "TechnologyUsed": "Django Framework"
                    },
                    {
                        "TechnologyUsed": "MongoDB in Backend"
                    }
                ]
            }
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5c992bd7330fd0aa0d2fe4cc"),
        ObjectId("5c992cdb330fd0aa0d2fe4cd")
    ]
}

Example: Search for "Java" Technology

To find records containing "Java" in the double nested array ?

db.doubleNestedArrayDemo.find({
    "StudentDetails.ProjectDetails.TechnologyUsed": "Java"
});
{
    "_id": ObjectId("5c992bd7330fd0aa0d2fe4cc"),
    "StudentId": "1000",
    "StudentName": "Larry",
    "StudentDetails": [
        {
            "ProjectName": "Online Banking",
            "ProjectDetails": [
                {
                    "TechnologyUsed": "Java"
                },
                {
                    "TechnologyUsed": "MySQL in Backend"
                }
            ]
        }
    ]
}

How It Works

  • StudentDetails is the first array level
  • ProjectDetails is the second (nested) array level
  • TechnologyUsed is the target field within the double nested structure
  • MongoDB automatically searches through all array elements at each level

Conclusion

Use dot notation with the pattern parentArray.childArray.fieldName to search within double nested arrays. MongoDB traverses all array elements automatically to find matching documents.

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

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements