Mongodb按照日期分组统计



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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM