I'm using mongoose.js to do queries to mongodb, but I think my problem is not specific to mongoose.js.
Say I have only one record in the collection:
var album = new Album({
tracks: [{
title: 'track0',
language: 'en',
},{
title: 'track1',
language: 'en',
},{
title: 'track2',
language: 'es',
}]
})
I want to select all tracks with language field equal to 'en', so I tried two variants:
Album.find({'tracks.language':'en'}, {'tracks.$': 1}, function(err, albums){
and tied to to the same thing with $elemMatch projection:
Album.find({}, {tracks: {$elemMatch: {'language': 'en'}}}, function(err, albums){
in either case I've got the same result:
{tracks:[{title: 'track0', language: 'en'}]}
selected album.tracks contain only ONE track element with title 'track0' (but there should be both 'track0', 'track1'):
{tracks:[{title: 'track0', language: 'en'}, {title: 'track1', language: 'en'}]}
What am I doing wrong?