Reputation: 371
This is the doc structure where offers and bids is sub-documents. I use mongoosejs as ORM.
var user = {
name : 'polk',
offers: [{
title: '1',
tags: ['a', 'b', 'c'],
bids: [{
_id: 1
name: 'jose',
age: 18
}, {
_id: 2
name: 'carlos',
age: 19
}]
},{
title: '2',
tags: ['d', 'e', 'f'],
bids: [{
_id: 3
name: 'gomes',
age: 21
}, {
_id: 4
name: 'fernando',
age: 31
}]
}]
};
What i need? This sub-document:
{
_id: 4
name: 'fernando',
age: 31
}
Using? Aggregation framework.
Upvotes: 0
Views: 880
Reputation: 26032
You can do it as follows with Aggregation Framework :
db.collection.aggregate(
{$unwind : "$offers"},
{$unwind : "$offers.bids"},
{$match : {"offers.bids._id" : 4}},
{$project : {_id : "$offers.bids._id",
name : "$offers.bids.name",
age : "$offers.name.age"
}}
)
Upvotes: 2