MongoDB retrieves selected objects from subdocuments

Is it possible to query specific objects inside a subdocument? Here is an example

Collection : Threads
{
    Documents : Messages
    {
        threadId = 1
        messages = [
            {
                user = amy
                date = 01/01/2012
                content = hey
            },
            {
                user = bell
                date = 01/01/2012
                content = hey
            },
            {
                user = bell
                date = 01/02/2012
                content = whats up
            }
        ]
    },
    {
        threadId = 2
        messages = [
            {
                user = courtney
                date = 01/03/2012
                content = first!
            }
        ]
    }
}

I want my request to say { threadId : 1, 'messages.date' : { $gt : 01/01/2012 } }, { fields : { messages : 1 } }. But he will return all messages of these documents when really everything I need, as a result,

messages = [
    {
        user = bell
        date = 01/02/2012
        content = whats up
    }
]
+5
source share
1 answer

You cannot return only the selected subdocument . You will get them all. Therefore, you will have to filter on the client side.

+6
source

All Articles