Следующий пример отлично работает.
MatchOperation matchStage = mongodbConstructorQueryUtils.makeMatchStage(topCriteria);
GroupOperation groupStage = Aggregation.group("teamId", "teamName")
.sum("shotsOfOneAttempted").as("sumShotsOfOneAttempted")
.sum("shotsOfTwoAttempted").as("sumShotsOfTwoAttempted")
.sum("shotsOfThreeAttempted").as("sumShotsOfThreeAttempted")
.addToSet("idMatchCallExt").as("matches");
ProjectionOperation projectionOperation = Aggregation.project("matches")
.and("sumShotsOfOneAttempted").as("sumShotsOfOneAttempted")
.and("sumShotsOfTwoAttempted").as("sumShotsOfTwoAttempted")
.and("sumShotsOfThreeAttempted").as("sumShotsOfThreeAttempted")
.and("matches").size().as("sumMatches");
Aggregation agg = Aggregation.newAggregation(
matchStage,
groupStage,
projectionOperation
);
Пример с циклами for:
MatchOperation matchStage = mongodbConstructorQueryUtils.makeMatchStage(topCriteria);
GroupOperation groupStage = Aggregation.group("teamId", "teamName");
for(String typeOfShots : typesOfShots) {
groupStage.sum(typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted");
}
groupStage.addToSet("idMatchCallExt").as("matches");
ProjectionOperation projectionOperation = Aggregation.project("matches");
for(String typeOfShots : typesOfShots) {
projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted");
}
Aggregation agg = Aggregation.newAggregation(
matchStage,
groupStage,
projectionOperation
);
Не работает.Он просто создает groupStage с teamId и teamName, и projectionOperation не может найти совпадения и т. Д. ...
Мои зависимости от spring.mongodb:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Знаете ли вы, почему это не так?т работа?