Database
db={Locations:[{"_id":"5e179fdbf1e0220def7a2b5c","name":"Cool Location A"},{"_id":"5e17a001f1e0220def7a2b5d","name":"Cool Location B"},{"_id":"5e17a015f1e0220def7a2b5e","name":"Cool Location C"},{"_id":"5e17a01cf1e0220def7a2b5f","name":"Cool Location D"},{"_id":"5e17a024f1e0220def7a2b60","name":"Cool Location E"},{"_id":"5e19d1d18f99a9b0f02836d3","name":"Cool Location F"}],Orders:[{"_id":"5e17ab585eb7f11a971bce5c","products":[{"name":"Product A Name","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"1"},{"name":"Product B Name","price":"400","_id":"5e0ea698ecfc75faece5dce7","amount":"2"}],"location":"5e17a001f1e0220def7a2b5d","total":"1000"},{"_id":"5e17ace34c79721cfd6cf657","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"2"},{"name":"Product B Name","price":"400","_id":"5e0ea698ecfc75faece5dce7","amount":"2"},{"name":"Product C Name","price":"200","_id":"5e0ea6a4ecfc75faece5dce8","amount":"1"}],"location":"5e179fdbf1e0220def7a2b5c","total":"1400"},{"_id":"5e17de81e59d563946b86495","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"2"},{"name":"Product B Name","price":"400","_id":"5e0ea698ecfc75faece5dce7","amount":"3"}],"location":"5e17a001f1e0220def7a2b5d","total":"1600"},{"_id":"5e17de89e59d563946b86496","products":[{"name":"Product D Name","price":"100","_id":"5e0ea6a8ecfc75faece5dce9","amount":"4"},{"name":"Product C Name","price":"200","_id":"5e0ea6a4ecfc75faece5dce8","amount":"3"},{"name":"Product B Name","price":"400","_id":"5e0ea698ecfc75faece5dce7","amount":"2"}],"location":"5e17a001f1e0220def7a2b5d","total":"2000"},{"_id":"5e19b914a8316aa4410f757c","products":[{"name":"Product B Name","price":"400","_id":"5e0ea698ecfc75faece5dce7","amount":"1"},{"name":"Product C Name","price":"200","_id":"5e0ea6a4ecfc75faece5dce8","amount":"1"}],"location":"5e17a001f1e0220def7a2b5d","total":"600"},{"_id":"5e19b94c97f06ea49bc05880","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"1"},{"name":"Product C Name","price":"200","_id":"5e0ea6a4ecfc75faece5dce8","amount":"1"}],"location":"5e17a001f1e0220def7a2b5d","total":"400"},{"_id":"5e19ee9c397745cb44b9550a","products":[{"id":"1","amount":"1","total":"20"},{"id":"2","amount":"1","total":"40"},{"id":"2","amount":"1","total":"40"},{"id":"2","amount":"1","total":"40"}],"location":"5e17a001f1e0220def7a2b5d","status":{"$numberInt":"1"},"total":"1000"},{"_id":"5e19ef22625e98cbf3cc5948","products":[{"id":"1","amount":"1","total":"20"},{"id":"2","amount":"1","total":"40"},{"id":"2","amount":"1","total":"40"},{"id":"2","amount":"1","total":"40"}],"location":"5e17a001f1e0220def7a2b5d","status":{"$numberInt":"1"},"total":"1000"},{"_id":"5e1b6f5ab17680190cf1e85c","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"1"},{"name":"Product C Name","price":"200","_id":"5e0ea6a4ecfc75faece5dce8","amount":"3"}],"location":"5e17a001f1e0220def7a2b5d","total":"800"},{"_id":"5e1b6fdba9b91d22fff1eeec","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"8"}],"location":"5e17a001f1e0220def7a2b5d","total":"1600"},{"_id":"5e1b6fe7a9b91d22fff1eeed","products":[{"name":"Product Name A","price":"200","_id":"5e0ea68eecfc75faece5dce6","amount":"12"}],"location":"5e17a001f1e0220def7a2b5d","total":"2400"},{"_id":"5e1c194372eda231600012ae","products":[{"name":"Product A Name","price":"100","_id":"5e1480206ae71b1e49389988","amount":"2"}],"location":"5e179fdbf1e0220def7a2b5c","total":"200"}]}
Query
db.Locations.aggregate([{$lookup:{from:"Orders",let:{loc_id:"$_id"},pipeline:[{$match:{$expr:{$eq:["$$loc_id","$location"]}}},{$project:{_id:0,products:1}}],as:"orders"}},{$project:{_id:1,name:1,products:"$orders.products"}}])