想象我有一个这样的集合:
{
"_id": "10280",
"city": "NEW YORK",
"state": "NY",
"departments": [
{"departmentType":"01",
"departmentHead":"Peter"},
{"departmentType":"02",
"departmentHead":"John"}
]
},
{
"_id": "10281",
"city": "LOS ANGELES",
"state": "CA",
"departments": [
{"departmentType":"02",
"departmentHead":"Joan"},
{"departmentType":"03",
"departmentHead":"Mary"}
]
},
{
"_id": "10284",
"city": "MIAMI",
"state": "FL",
"department": [
"departments": [
{"departmentType":"01",
"departmentHead":"George"},
{"departmentType":"02",
"departmentHead":"Harry"}
]
}
我想获得每个部门类型的计数,例如:
[{"departmentType":"01", "dCount":2},
{"departmentType":"02", "dCount":3},
{"departmentType":"03", "dCount":1}
]
为此,我已经尝试了几乎所有方法,但是我在网上找到的所有示例都比较简单,其中 group by 是在文档根级别的字段上完成的。相反,我在这里尝试按部门类型进行分组,这似乎破坏了我迄今为止发现的所有内容。
关于如何使用 Mongoose 的聚合实现或 mapreduce 来做到这一点的任何想法?
理想情况下,我想排除所有 count <= 1 的部门类型,并按部门类型对结果进行排序。
谢谢大家!