U moet $unwind
de array "sizes" en vervolgens $group
door "item" en "sizes" en gebruik de $sum
om de telling terug te geven.
db.collection.aggregate(
[
{ "$unwind": "$sizes" },
{ "$group": {
"_id": {
"item": "$item",
"sizes": "$sizes"
},
"count": { "$sum": 1 }
}}
]
)
die produceert:
{ "_id" : { "item" : "EFG", "sizes" : "S" }, "count" : 1 }
{ "_id" : { "item" : "ABC", "sizes" : "L" }, "count" : 1 }
{ "_id" : { "item" : "ABC", "sizes" : "M" }, "count" : 2 }
{ "_id" : { "item" : "ABC", "sizes" : "S" }, "count" : 1 }