我使用以下聚合来计算具有特定“alertsources.date_creation”的文档“alert”上的“alertsources”,但我不知道为什么它计算所有alertsources而不是那些具有条件的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")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句