本文介绍了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聚合
发布评论