Mongoose: Populating object's nested array with projection
You can do this by providing the select
option in your populate
call:
Car.findOne() .populate({ path: 'partIds', select: { props: { $elemMatch: { colour: 'red' } }, name: 1 } }) .exec(callback);
Result:
{ _id: 57c085451cd8dfcdf814f640, name: 'BMW', partIds: [ { _id: 57baa43e152654f80aac36a6, name: 'Piston', props: [ { colour: 'red', shape: 'Cubical' } ] } ] }
The select
uses the $elemMatch
projection operator to select just the red props
element.
you can try this, it should get you what you want.
car.find({"partIds.props.colour" : "red"}) .populate('partIds') .exec(function(err,result){...});