Skip to content

Instantly share code, notes, and snippets.

@gharriso
Created February 15, 2018 00:23
Show Gist options
  • Save gharriso/52e47499ed32e837b3da138f86fe70bb to your computer and use it in GitHub Desktop.
Save gharriso/52e47499ed32e837b3da138f86fe70bb to your computer and use it in GitHub Desktop.
db.getCollection("lineitems").aggregate([
{ $group:{ _id:{ "prodId":"$prodId" },
"itemCount-sum":{$sum:"$itemCount"} }
},
{ $sort:{ "itemCount-sum":-1 }},
{ $limit: 10 },
{ $lookup:
{ from: "products",
localField: "_id.prodId",
foreignField: "_id",
as: "product"
}
},
{ $project: {
"ProductName": "$product.productName" ,
"itemCount-sum": 1 ,
"_id": 1
}
},
],{allowDiskUse: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment