How build Spring Data Mongo Aggregation Operations using for-loop

我是研究僧i 提交于 2019-12-13 03:58:14

问题


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?


回答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");
}


来源:https://stackoverflow.com/questions/54709869/how-build-spring-data-mongo-aggregation-operations-using-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!