Spring Data Mongo DB聚合

编程入门 行业动态 更新时间:2024-10-26 02:29:44
本文介绍了Spring Data Mongo DB聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用以下汇总对具有特定名称"alertsources.date_creation"的文档"alert"中的"alertsources"进行计数,但是我不知道为什么它计算所有警报源而不是那些具有条件的警报源:

I use the following aggregation to count "alertsources" on document "alert" that has a specifique "alertsources.date_creation" but I don't know why it counts all the alertsources instead of those who has the criteria :

final Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("alertsources.date_creation").regex(date)), Aggregation.match(Criteria.where("descA").is(alertName)), //regex(".*"+date+".*") Aggregation.unwind("alertsources"), Aggregation.unwind("descA"), Aggregation.group().count().as("count")); //System.out.println("----------"+mongoTemplate.aggregate(aggregation, Alert.class, MentionCount.class).getRawResults()+"-----"); List<MentionCount> agregResult = mongoTemplate.aggregate(aggregation, Alert.class, MentionCount.class).getMappedResults();

推荐答案

我解决了这个问题,我应该在$ unwind之前和之后应用$ match:

I solved the problem, I should have applied $match before and after $unwind :

Aggregation.match(Criteria.where("alertsources.date_creation").regex(".*"+date+".*")), Aggregation.match(Criteria.where("descA").is(alertName)), //regex(".*"+date+".*") Aggregation.unwind("alertsources"), Aggregation.unwind("descA"), Aggregation.match(Criteria.where("alertsources.date_creation").regex(".*"+date+".*")), Aggregation.group().count().as("count")

所有功劳归功于@ Neil Lunn ,研究后,我发现了他的原始answer .

All the credit goes to @Neil Lunn , after doing research I found his original answer on the matter.

更多推荐

Spring Data Mongo DB聚合

本文发布于:2023-11-22 18:46:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Data   Spring   DB   Mongo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!