MongoDB从嵌套文档中检索所选对象

时间:2012-06-27 05:31:12

标签: mongodb

是否可以在嵌套文档中查询特定对象?这是一个例子,

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!
            }
        ]
    }
}

我希望我的查询说{ threadId : 1, 'messages.date' : { $gt : 01/01/2012 } }, { fields : { messages : 1 } }。但它会返回所有文档消息,因为我真正想要的就是这个,

messages = [
    {
        user = bell
        date = 01/02/2012
        content = whats up
    }
]

1 个答案:

答案 0 :(得分:8)

You cannot return just the selected subdocument。你会得到所有这些。因此,您必须在客户端进行过滤。