Du måste $unwind
objekt array före gruppering:
const aggregatorOpts = [{
$unwind: "$items"
},
{
$group: {
_id: "$items.productId",
count: { $sum: 1 }
}
}
]
Model.aggregate(aggregatorOpts).exec()
vilket ger:
{ "_id" : "789", "count" : 1 }
{ "_id" : "456", "count" : 2 }
{ "_id" : "123", "count" : 3 }