如何使用 for 循环构建 Spring Data Mongo 聚合操作 [英] How build Spring Data Mongo Aggregation Operations using for-loop

查看:49
本文介绍了如何使用 for 循环构建 Spring Data Mongo 聚合操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例完美运行.

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

它不起作用.它只是使用teamId和teamName构建groupStage,并且projectionOperation无法找到匹配项等等......

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

我对 spring.mongodb 的依赖:

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?

推荐答案

您的项目和组操作都存在相同的问题.我以项目操作为例.

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

您的方法 projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted"); 将返回一个 ProjectOperation.但是您没有将结果保存在变量中,因此聚合管道仅执行 Aggregation.project("matches");.

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.

你可以试试

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

for(String typeOfShots : typesOfShots) {
    projectionOperation = projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted");
}

这篇关于如何使用 for 循环构建 Spring Data Mongo 聚合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆