Boat
Boat

Reputation: 535

mongo aggregation to elemMatch on nested arrays

I have a document like;

{
    "name": "abc"
    "array1": [
        {
            "_id": "87a015b3-b81c-4c75-8dd6-4fa0bd6d9660",
            "array2": [
                [
                   {
                        "_id": "123",    
                        "values": [{
                                "value": "ABC"
                            }
                        ]
                    }, 
                    .....
                    .....
                ],

                [
                    {
                        "_id": "789",
                        "values": [{
                                "value": "XYZ"
                            }
                        ]
                    }, 
                    .....
                    .....
                ]
            ]
        }
    ]
}

How can we do $elemMatch to filter based on _id and values? For example, match if there is an entry with "_id" = "123" and "value" = "ABC" inside array1.array2 .

Upvotes: 0

Views: 491

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

You can do this:

db.collection.find(
{"array1.array2": {$elemMatch: {$elemMatch: {_id: "123", "values.value": "ABC"}}}}
)

I personally recommend you guys reconsider you're document structure as currently i feel like it does not make much sense to me.

Upvotes: 1

Related Questions