简体   繁体   English

Spring Data Mongo数据库聚合

[英]spring data Mongo db aggregation

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 : 我使用以下聚合对具有特定规范“ alertsources.date_creation”的文档“ alert”中的“ alertsources”进行计数,但是我不知道为什么它计算所有警报源而不是那些具有条件的警报源:

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();

I solved the problem, I should have applied $match before and after $unwind : 我解决了这个问题,我应该在$ unwind之前和之后应用$ match:

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")

All the credit goes to @ Neil Lunn , after doing research I found his original answer on the matter. 所有的功劳归功于@ Neil Lunn ,经过研究,我找到了他对此事的原始答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM