【发布时间】:2020-03-29 10:24:06
【问题描述】:
我有一个如下结构的文档。
{
name: "John Doe",
City : "OK",
Prepaid: "Y"
},
{
name: "Jane Doe",
City : "CA",
Prepaid: "N"
},
{
name: "Jule Doe",
City : "OK",
Prepaid: "N"
},
{
name: "Jake Doe",
City : "OK",
Prepaid: "Y"
}
我想先根据城市对它进行分组,然后再对预付费进行分组,然后分别获取每种预付费类型的计数。看起来与此类似的东西。
{
City : OK
Count : {
"filter": prepaid,
"count": {
Y : 2
N: 1
}
}
}
{
City : CA
Count : {
"filter": prepaid,
"count": {
Y : 0
N: 1
}
}
}
我尝试基于多个字段进行聚合,它给了我文档的总数,而不是细分。
这是我为聚合管道尝试的:
db.collection.aggregate([
{$match:matchquery
},{$group:{_id:{city: '$city', prepaid: '$prepaid' }, count:{$sum:1}
}}
])
【问题讨论】:
标签: javascript mongodb aggregation-framework