mongodb做数据库,统计数据库的数据。按时间分组,统计每天|每月的客流量。数据库存放的时间是默认的时间格式,类似:Sun Jan 24 2016 20:39:57 GMT+0800 (CST)
。
问:这个时间怎么通过springboot 提供的mongo操作方法完成时间格式化呢?
目前使用了Aggregation,大致代码如下:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.group("addedDate").count().as("count"));
其中addDate
就是存放数据库的时间字段
聚合查询在分组前,先使用
aggregation
的project
将时间提取出来,类似Aggregation.project("要提取的字段").andExpression("数据库时间字段").extractDayOfYear().as("day")
,然后在通过Aggregation.group("day").count().as("count"));
这样来分组。