想象我有這樣一個集合:的MapReduce /聚合:集團由值嵌套文檔中
{
"_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計數,像這樣:
[{"departmentType":"01", "dCount":2},
{"departmentType":"02", "dCount":3},
{"departmentType":"03", "dCount":1}
]
爲此,我已經嘗試了幾乎所有的東西,但我在網上找到的所有示例都比較容易,其中group by是在文檔根級別的字段上完成的。相反,在這裏我試圖按部門類型進行分組,這似乎破壞了我發現的所有東西。
有關如何使用Mongoose的聚合實現或mapreduce來做到這一點的任何想法?
理想情況下,我想排除所有與< = 1的部門類型和按部門類型排序結果。
謝謝大家提前!
你確定上一個部門是否正確?它看起來不正確。 – cirrus