I have the following Schema Structure
Artist Schema
albums:[Albums Schema]
Album Schema
songs:[Songs Schema]
Now, i want to search by a song id and return the song, its parent album and the artist detail. I don't want to return a song and album other than the one matched. So far i have only been able to restrict it to returning the queried album using the positional or elemMatch but it still returns all the songs . Does anyone know a way of restricting both the song and the album elements to one ?
Examples below are using the mongo shell for convenience but should be
straightforward to translate.
Assuming you have a schema similar to:
db.artist.insert({
name: '"Weird Al" Yankovic',
albums: [
{
name: 'Dare to Be Stupid',
year: 1995,
songs: [
{ track: 'A1', name: 'Like a Surgeon' },
{ track: 'A2', name: 'Dare to Be Stupid' },
{ track: 'A3', name: 'I Want a New Duck' },
],
},
{
name: 'Polka Party!',
year: 1985,
songs: [
{ track: 'A1', name: 'Living with a Hernia' },
{ track: 'A2', name: 'Dog Eat Dog' },
{ track: 'A3', name: 'Addicted to Spuds' },
],
},
],
})
You can use the Aggregation Framework to get the expected results:
db.artist.aggregate(
// First match to reduce the number of candidate documents
{ $match: {
"albums.songs.name": "I Want a New Duck",
}},
// Unwind 'albums' & 'songs' arrays
{ $unwind: "$albums" },
{ $unwind: "$albums.songs" },
// Limit to matching song & album
{ $match: {
"albums.songs.name": "I Want a New Duck",
}}
)
Example result:
{
"_id" : ObjectId("53defb826ed136423e50d082"),
"name" : "\"Weird Al\" Yankovic",
"albums" : {
"name" : "Dare to Be Stupid",
"year" : 1995,
"songs" : {
"track" : "A3",
"name" : "I Want a New Duck"
}
}
}