Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

Springboot+jpa+mongodb中关于Aggregation分组求平均如何处理?求各位指教

此处原先是根据name和code分组,现在想在在分组的基础上添加一个dateTime分组。数据库中的分组是精确到秒的,此处需要精确到小时对数据库中的value字段按小时求平均值。下面是原先的代码,请问如何对datetime字段做处理并求平均值?

 AggregationResults<MongoCount> countAggregation = mongoTemplate.aggregate(
                Aggregation.newAggregation(
                        Aggregation.match(matchCriteria),
                        //下面分组想要添加一个dateTime字段,该字段在数据库中是到秒,此处的需求是按照name+code+dateTime到小时分组求平均值
                        //请问这里怎么修改
                        Aggregation.group("name", "code").push("$$ROOT").as("results"),
                        Aggregation.group().count().as("count")
                ), "mongo_data", MongoCount.class);
MongoCount mongoCount = countAggregation.getUniqueMappedResult();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...