Sie müssen $unwind
items-Array vor der Gruppierung:
const aggregatorOpts = [{
$unwind: "$items"
},
{
$group: {
_id: "$items.productId",
count: { $sum: 1 }
}
}
]
Model.aggregate(aggregatorOpts).exec()
was ergibt:
{ "_id" : "789", "count" : 1 }
{ "_id" : "456", "count" : 2 }
{ "_id" : "123", "count" : 3 }