1

Having the following document in my Mongo I'm trying to get the object with specified id. Here is my Mongo document. Mongo version: 2.6

{
    "_id" : ObjectId("57c1ae9ac1bd31d4eb4d546d"),
    "footers" : [ 
        {
            "type" : "web",
            "rows" : [ 
                {
                    "id" : "abc",
                    "elements" : [ 
                        {
                            "id" : "def",
                            "type" : "image",
                            "url" : "http://example.com"
                        }, 
                        {
                            "id" : "ghi",
                            "type" : "image",
                            "url" : "http://example.com"
                        }
                    ]
                }
            ]
        }
    ]
}

I'm looking for an object with id "def", and I want to obtain this result:

{
    "id" : "def",
    "type" : "image",
    "url" : "http://example.com"
}

Below I cite the example of code that I tried to make search of this object.

db.getCollection('myCollection').aggregate([
    {"$match": {
        "footers.rows.elements.id": "def"
    }},
    {"$group": {
        "_id": "$footers.rows.elements"
    }}
])

And the result is:

{
    "_id" : [ 
        [ 
            [ 
                {
                    "id" : "def",
                    "type" : "image",
                    "url" : "http://example.com"
                }, 
                {
                    "id" : "ghi",
                    "type" : "image",
                    "url" : "http://example.com"
                }
            ]
        ]
    ]
}

Any suggestions?

1 Answer 1

2

You need to use "$unwind".

This answer will help you with more details Mongodb unwind nested documents (https://stackoverflow.com/a/12241733/224743 specifies this should work in MongoDB 2.2+)

For your specific example, you could do something like:

db.getCollection('myCollection').aggregate([
    {"$match"  : { "footers.rows.elements.id": "def" }}, 
    {"$unwind" : "$footers"}, 
    {"$unwind" : "$footers.rows"}, 
    {"$unwind" : "$footers.rows.elements"}, 
    {"$group"  : { "_id": "$footers.rows.elements" }}, 
    {"$match"  : { "_id.id": "def" }}
]);

Notice the multiple "$unwind" chainings and also the final "$match" which is needed to reapply the conditions for the $unwind-ed documents.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.