mongodb按时间分组

mongodb集合按天分组三种形式


时间字段是Long类型时间戳

1.时间戳转换成String类型,在转成Date,去掉时分秒mongodb

Aggregation.project().andExpression("dateToString('%Y-%m-%d', add([0],'$createTime',28800000))",new Date(0)).as("drinkTime")

2.时间戳去掉时分秒ide

Aggregation.project().andExpression("floor(divide($createTime,86400000))").as("groupCreateTime")

时间字段是Date类型,能够利用mongodb自带函数year()month()monthOfDay()分组

Aggregation.project("_id")
                .andExpression("year(drinkTime)").as("year")
                .andExpression("month(drinkTime)").as("month")
                .andExpression("dayOfMonth(drinkTime)").as("day")
相关文章
相关标签/搜索