spring数据Mongo db聚合

时间:2017-08-24 10:52:51

标签: spring mongodb

我使用以下聚合来计算具有“alertsources.date_creation”的文档“alert”上的“alertsources”,但我不知道为什么它会计算所有alertource而不是那些具有条件的那些:

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

1 个答案:

答案 0 :(得分:0)

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

{{1}}

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

相关问题