1

I have this parto of query in mongo that return an array of fields.

{ $project:
    {
        ...
        result:
            [
                { sumShotsOfOneAttempted: '$sumShotsOfOneAttempted1', sumShotsOfOneFailed: '$sumShotsOfOneFailed1'},
                { sumShotsOfOneAttempted: '$sumShotsOfOneAttempted2', sumShotsOfOneFailed: '$sumShotsOfOneFailed2'}
            ]

    }
}

I'm trying to build the same with spring-mongodb using ProjectionOperation:

ProjectionOperation projectStage = Aggregation.project("fieldId");

And I want to add the fields inside of array to this projectStage, but I only know how to add one by one as projectStage.and("sumShotsOfOneAttempted1").as(sumShotsOfOneAttempted); but not arrays.

My depndendencies of spring.mongodb:

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

Any idea please?

vlarios
  • 31
  • 8

1 Answers1

1

As mentioned in you other post enter link description here

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

for(String typeOfShots : typesOfShots) {
    projectionOperation = projectionOperation.and("sum"+typeOfShots+"Attempted").as("sum"+typeOfShots+"Attempted");
}
charlycou
  • 1,778
  • 13
  • 37