Say I have 3 documents:
[
{
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'
}
]
}
]
My desired query is as follows:
- Find all documents where
auth: true
(all 3). - Then I only want to return the "sources" where
user: 123
.
That is to say, I want to return the ENTIRE document where Auth is true, but within those documents I only want to return the sources where the "user" is "123".
Something like db.users.find({auth:true, 'sources.user': '123'}) obviously returns whole documents with a matching
source.user`. I need something like:
db.users.find({auth:true}, {'source.user: "123"': 1})
or the like. Perhaps this can be done with map reduce? Any ideas?