Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 2x 2x 2x | import ProductsModel from "../../models/Products.js";
export default async function categoriesStatistics() {
try {
const categories = await ProductsModel.aggregate([
{
$group: {
_id: "$category",
category: { $first: "$category" },
productsCount: { $count: {} },
totalEarnings: { $sum: "$earnings" },
productsSold: { $sum: "$sold" }
}
},
{ $unset: ["_id"] }
])
return categories;
} catch (error) {
console.log(error)
return null;
}
} |