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?

1 Answer 1

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