I use mongoose.js to execute requests to mongodb, but I think my problem is not specific to mongoose.js.
Say I have only one entry 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 a language field equal to 'en', so I tried two options:
Album.find({'tracks.language':'en'}, {'tracks.$': 1}, function(err, albums){
and attached to the same with the $ elemMatch projection:
Album.find({}, {tracks: {$elemMatch: {'language': 'en'}}}, function(err, albums){
Anyway, I have the same result:
{tracks:[{title: 'track0', language: 'en'}]}
selected album.tracks contain only one track element named "track0" (but there must be both "track0", "track1"):
{tracks:[{title: 'track0', language: 'en'}, {title: 'track1', language: 'en'}]}
What am I doing wrong?