Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB retrieving selected objects from nested documents [duplicate]

Tags:

mongodb

Is it possible to query for specific objects within a nested document? Heres 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 would like my query to say { threadId : 1, 'messages.date' : { $gt : 01/01/2012 } }, { fields : { messages : 1 } }. But it will return all of that documents messages when really all i want as a result is this,

messages = [
    {
        user = bell
        date = 01/02/2012
        content = whats up
    }
]
like image 380
cnotethegr8 Avatar asked Jun 27 '12 05:06

cnotethegr8


1 Answers

You cannot return just the selected subdocument. You'll get all of them. So you'll have to filter on the client side.

like image 136
Thilo Avatar answered Oct 05 '22 12:10

Thilo