我正在尝试汇总管道以根据年份和月份对集合中的文档进行分组和计数。到目前为止,我有这个:
db.cars.aggregate()
.match({"archived_at": { $lt: new ISODate("") }})
.group({
_id: {month: {$month:"$archived_at"}, year: {$year:"$archived_at"}},
count:{$sum:1}
})
返回以下内容:
{"_id":{"month":9,"year":2019},"count":80}
{"_id":{"month":8,"year":2019},"count":30}
{"_id":{"month":7,"year":2019},"count":22}
{"_id":{"month":3,"year":2019},"count":11}
{"_id":{"month":5,"year":2019},"count":5}
{"_id":{"month":4,"year":2019},"count":2}
{"_id":{"month":6,"year":2019},"count":54}
有没有一种方法可以将这些数据转换成这样
{
"2019": {
"1": "30",
"2": "80"
}
"2018": {
"1": "4",
"2": "7"
}
}
尝试了几件事,但是没有用,所以我第一次寻求帮助。
Thx