MongoDB从嵌套文档中检索选定对象
是否可以在嵌套文档中查询特定对象?举个例子MongoDB从嵌套文档中检索选定对象,mongodb,Mongodb,是否可以在嵌套文档中查询特定对象?举个例子 Collection : Threads { Documents : Messages { threadId = 1 messages = [ { user = amy date = 01/01/2012 content = hey },
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
}
]
。你会得到所有的。因此,您必须在客户端进行筛选 使用MongoDB聚合管道可以为您提供一个解决方案!