Collections structure :
- component_list
- component_sold
- registered_buyer
Code :
// line 1 pake db.collection.aggregate juga bisa, optional aja CollectionModel.aggregate( { $lookup: { from: "component_list", localField: "component_id", foreignField: "_id", as: "unit" } }, { $unwind: "$unit" }, { $lookup: { from: "registered_buyer", localField: "buyer_id", foreignField: "_id", as: "buyer" } }, { $unwind: "$buyer" }, { $group: { "_id": { "name": "$unit.name", "brand": "$unit.brand" }, "total_sold": { $sum: "$quantity" } } }, { $project: { "_id": 1, "unit": { "name": "$_id.name", // dapet dari group, redefine $unit.name ga bakal work "brand": "$_id.brand" // sama, dapet dari group }, "buyer": { "name": "$buyer.name", "address": "$buyer.address", "telp": "$buyer.telp" }, "total_sold": "$total_sold" // dapet dari group } }
code source from : https://github.com/noric1902/express_b/blob/master/routes/app.js
0 comments:
Post a Comment