簡體   English   中英

Spring Data Mongo數據庫聚合

[英]spring data Mongo db aggregation

我使用以下聚合對具有特定規范“ 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();

我解決了這個問題,我應該在$ 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")

所有的功勞歸功於@ Neil Lunn ,經過研究,我找到了他對此事的原始答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM