Anda perlu $unwind
larik "sizes" lalu $group
oleh "item" dan "ukuran" dan gunakan $sum
untuk mengembalikan hitungan.
db.collection.aggregate(
[
{ "$unwind": "$sizes" },
{ "$group": {
"_id": {
"item": "$item",
"sizes": "$sizes"
},
"count": { "$sum": 1 }
}}
]
)
yang menghasilkan:
{ "_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 }