So I want to use pipeline aggregation in MongoDB to query certain values from documents and then add them together.
My "Albums" document.
{
"_id" : ObjectId("5875ed1dc939408da0601f31"),
"AlbumName" : "Blurryface",
"Artist" : "21 Pilots",
"Date" : "20151110",
"Label" : "Fueled By Ramen",
"Writers" : "Tyler Joseph",
"Producer" : "Mike Elizondo",
"Songlist" : [
{
"_id" : ObjectId("5875e5e8c939408da0601d73"),
"SongID" : "1",
"SongName" : "Stressed Out",
"Artist" : "21 Pilots",
"Album" : "Blurryface",
"Duration:" : "200",
"nPlays" : 800000000,
"SongDataFile" : "data"
},
{
"_id" : ObjectId("5875e855c939408da0601dcc"),
"SongID" : "4",
"SongName" : "Heathens",
"Artist" : "21 Pilots",
"Album" : "Blurryface",
"Colaborator" : "NA",
"Duration:" : "320",
"nPlays" : 5000000,
"SongDataFile" : "data"
}
]
}
How can I make an aggregation pipeline that extracts the "nPlays" from the songs in the array and then add them together?
I'm asking here since the documentation on MongoDB is subpar and they have no examples of how to use the operators together. Add to this that all examples on google only query for $gt $lt or use the same example that only uses $match and $group which doesn't help me with my problem at all.
In short:
How do I extract "nPlays" and add them together in a pipeline aggregation?