首页 > 数据库技术 > 详细

Mongodb按照日期分组统计

时间:2019-12-21 16:02:27      阅读:307      评论:0      收藏:0      [点我收藏+]

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")

Mongodb按照日期分组统计

原文:https://www.cnblogs.com/xibuhaohao/p/12076823.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!