【发布时间】:2020-03-30 09:02:51
【问题描述】:
我正在尝试计算文档中特定值的出现次数。我的文档结构如下:
id: ObjectID("ABC123")
StoreItems: Array
0: Object
Type: "Apple"
Color: "Green"
Size: "Small"
Weight: "5"
1: Object
Type: "Orange"
Color: "Orange"
Size: "Small"
Weight: "8"
2: Object
Type: "Grapes"
Color: "Green"
Size: "Small"
Weight: "8"
当我使用以下内容时,它只给我分组的总数,而不是每个特定类型:
[
{
'$unwind': {
'path': '$StoreItems'
}
}, {
'$match': {
'StoreItems.Color': 'Green'
}
}, {
'$group': {
'_id': 'Type',
'Count': {
'$sum': 1
}
}
}
]
我想得到一个结果:Apples: 1, Grapes:3
但是,我只得到一个结果:Type: 4
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework