说我有3个文件:
[
{
name: "Phil",
auth: true,
sources: [
{
user: '123',
area: 'PA'
},{
user: '456',
area: 'NYC'
}
]
},{
name: "Chris",
auth: true,
sources: [
{
user: '123',
area: 'SF'
}
]
},{
name: "Joel",
auth: true,
sources: [
{
user: '456',
area: 'LA'
}
]
}
]
我想要的查询如下:
auth: true
(全部3个)user: 123
。也就是说,我想返回Auth为真的整个文档,但在这些文档中我只想返回“user”为“123”的源。
像db.users.find({auth:true, 'sources.user': '123'}) obviously returns whole documents with a matching
source.user`这样的东西。我需要这样的东西:
db.users.find({auth:true}, {'source.user: "123"': 1})
等。也许这可以用map reduce完成?有什么想法吗?