鍍金池/ 問答/Java  數(shù)據(jù)庫/ springboot mongodb 按時間分組統(tǒng)計,時間格式化粒度到天

springboot mongodb 按時間分組統(tǒng)計,時間格式化粒度到天

mongodb做數(shù)據(jù)庫,統(tǒng)計數(shù)據(jù)庫的數(shù)據(jù)。按時間分組,統(tǒng)計每天|每月的客流量。數(shù)據(jù)庫存放的時間是默認的時間格式,類似:Sun Jan 24 2016 20:39:57 GMT+0800 (CST) 。

問:這個時間怎么通過springboot 提供的mongo操作方法完成時間格式化呢?

目前使用了Aggregation,大致代碼如下:

Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.group("addedDate").count().as("count"));

其中addDate就是存放數(shù)據(jù)庫的時間字段

回答
編輯回答
青裙

聚合查詢在分組前,先使用aggregationproject將時間提取出來,類似Aggregation.project("要提取的字段").andExpression("數(shù)據(jù)庫時間字段").extractDayOfYear().as("day"),然后在通過Aggregation.group("day").count().as("count"));這樣來分組。

2018年5月30日 17:00