目录
1. 使用时间格式化方法
2. 进行时间补偿 (默认当前时区是东八区, 即 8x3600x1000=28800000)
MongoDB 的默认时间是格林尼治时间, 如果是要按照日期进行分组需要注意!!!.
解决方案:
1. 使用时间格式化方法
- Aggregation.project().and("createTime").dateAsFormattedString("%Y-%m-%d").as("time"),
- Aggregation.group("time").count().as("count")
或者
- Aggregation.project().and(DateOperators.DateToString.dateOf("createTime").toString("%Y-%m-%d")).as("name"),
- Aggregation.group("time").count().as("count")
dateAsFormattedString() 方法会默认将当前时间转为系统默认的时区
2. 进行时间补偿 (默认当前时区是东八区, 即 8x3600x1000=28800000)
- Aggregation.project().andExpression("add(createTime,28800000)").as("time"),
- Aggregation.group("time").count().as("count")
来源: http://www.bubuko.com/infodetail-3343052.html