Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...