表的结构:
{
"point": "625fa3ccd3ebc0de0f1837d9",
"_id": "625fb7ad9c9d58cdc664fde5",
"create": "2022-04-20T07:35:09.203Z",
"update": "2022-04-20T07:35:09.203Z",
"__v": 0
}
现在需要按照时间,每分钟统计次数,得到如下 aggregate:
this.reportModel.aggregate([
{$match: {point:data[0]._id,}},
{$project: {
date1Str: {$dateToString: {format: "%Y-%m-%d %H:%M", date:"$create",timezone:"+08:00"}},
}},
{$group: {_id: "$date1Str", total: {$sum: 1}}},
{$sort:{_id:1}}
]).exec()
查出来的数据,只统计了有数据的日期
[
{
"_id": "2022-04-20 14:10",
"total": 8
},
{
"_id": "2022-04-20 14:11",
"total": 1
},
{
"_id": "2022-04-20 14:19",
"total": 1
},
{
"_id": "2022-04-20 14:20",
"total": 6
},
{
"_id": "2022-04-20 14:25",
"total": 8
},
{
"_id": "2022-04-20 15:16",
"total": 1
},
{
"_id": "2022-04-20 15:35",
"total": 1
}
]
如何实现没有数据的日期自动补 0 ,例如:
[
{
"_id": "2022-04-20 14:10",
"total": 8
},
{
"_id": "2022-04-20 14:11",
"total": 0
},
{
"_id": "2022-04-20 14:12",
"total": 0
},
{
"_id": "2022-04-20 14:11",
"total": 1
},
...
]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.