I have products collection with a “category” as array:
> { name: "ball", category: ["sport", "toys" ], quantity: 5 }
> { name: "gym bar", category: ["sport"], quantity: 3 }
How to aggregate by category in array ?
How to calc quantity of all products in each category ?
Expexted result - “sport”: 8, “toys”: 5
I tryed this …
.aggregate([
{$unwind: "$category"},
{$group: {category: "$category", quantity: {$sum: "$quantity"}}}
])
Error: “The field ‘category’ must be an accumulator object”