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

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

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

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

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

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

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

回答
編輯回答
青裙

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

2018年5月30日 17:00