I am creating asset managment tool for warehouse and I have Warehouse , Asset, Category models. I want to get the overall stats of each warehouse like , total assets and how many of them is active or in-store, so that I used aggregate but It seams to me more time consuming .
My question is what if I use populate
method over aggregate, I mean store the needed asset values in the warehouse model or is there any better option for this
This is how I got the status of assets in on warehouse
const assetStats = await Asset.aggregate([ { $group: { _id: id, totalAssets: { $sum: 1 }, inStore: { $sum: { $cond: [{ $eq: ["$status", "in-store"] }, 1, 0], }, }, deployed: { $sum: { $cond: [{ $eq: ["$status", "deployed"] }, 1, 0], }, }, inRepair: { $sum: { $cond: [{ $eq: ["$status", "in-repair"] }, 1, 0], }, }, }, }, ]);
Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.