0

The following example works perfectly.

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

Example with for-loops:

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

It doesn't work. It just build groupStage with teamId and teamName, and projectionOperation failds to found matches and so on...

My depndendencies of spring.mongodb:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

Do you know why it doesn't work?

2
  • Attempted should be "Attempted", am I wrong? Commented Feb 20, 2019 at 9:00
  • Yes, I'm gonna edit the question. Thx. Commented Feb 20, 2019 at 11:26

1 Answer 1

1

You have the same problem for both project and group operation. I'll take the porject operation as an example.

Your method projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted"); will return a ProjectOperation. But your not saving your result in your variable so only Aggregation.project("matches"); is executed by the aggregation pipeline.

Instead you could try

ProjectionOperation projectionOperation = Aggregation.project("matches");

for(String typeOfShots : typesOfShots) {
    projectionOperation = projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.