我有一个记录数据集,如下所示:
{
"_id": 1,"itemId": "t1","startDate": ISODate("2019-10-14T21:00:00.000+0000"),"endDate": ISODate("2019-10-16T21:00:00.000+0000"),"status": "TODO"
},{
"_id": 2,"startDate": ISODate("2019-10-17T21:00:00.000+0000"),"endDate": null,"status": "DONE"
}
,其中每个文档都包含项目的当前状态。如果文档具有endDate-表示该日期之前该文档的状态一直由该文档表示。如果endDate为null,则表示文档仍代表该商品的当前状态。
我想创建一个聚合,该聚合将每天计算每种状态下的项目数量。例如,对于上面的数据和范围14.10.2019-19.10.2019,结果将是:
{
"results":
[
{"date": ISODate("2019-10-14T21:00:00.000+0000"),"TODO": 1,"DOING": 0,"DONE": 0},{"date": ISODate("2019-10-15T21:00:00.000+0000"),{"date": ISODate("2019-10-16T21:00:00.000+0000"),{"date": ISODate("2019-10-17T21:00:00.000+0000"),"TODO": 0,"DOING": 1,{"date": ISODate("2019-10-18T21:00:00.000+0000"),{"date": ISODate("2019-10-19T21:00:00.000+0000"),"DONE": 0}
]
}
我正在寻找一种映射数据的方法,并且为每个文档运行以下逻辑:
if (startDate <= currentDate) && (endDate == null || endDate > currentDate) -> add 1 to currentDate's status count for this day and status.
谢谢!