Example usage for org.springframework.data.mongodb.core.aggregation ArithmeticOperators valueOf

List of usage examples for org.springframework.data.mongodb.core.aggregation ArithmeticOperators valueOf

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.aggregation ArithmeticOperators valueOf.

Prototype

public static ArithmeticOperatorFactory valueOf(AggregationExpression expression) 

Source Link

Document

Take the value resulting from the given AggregationExpression .

Usage

From source file:example.springdata.mongodb.aggregation.SpringBooksIntegrationTests.java

/**
 * Retrieve the number of pages per author (and divide the number of pages by the number of authors).
 *///from www.  j  av  a  2 s .  com
@Test
public void shouldRetrievePagesPerAuthor() {

    Aggregation aggregation = newAggregation( //
            match(Criteria.where("volumeInfo.authors").exists(true)), //
            replaceRoot("volumeInfo"), //
            project("authors", "pageCount") //
                    .and(ArithmeticOperators.valueOf("pageCount") //
                            .divideBy(ArrayOperators.arrayOf("authors").length()))
                    .as("pagesPerAuthor"),
            unwind("authors"), //
            group("authors") //
                    .sum("pageCount").as("totalPageCount") //
                    .sum("pagesPerAuthor").as("approxWritten"), //
            sort(Direction.DESC, "totalPageCount"));

    AggregationResults<PagesPerAuthor> result = operations.aggregate(aggregation, "books",
            PagesPerAuthor.class);

    PagesPerAuthor pagesPerAuthor = result.getMappedResults().get(0);

    assertThat(pagesPerAuthor.getAuthor()).isEqualTo("Josh Long");
    assertThat(pagesPerAuthor.getTotalPageCount()).isEqualTo(1892);
    assertThat(pagesPerAuthor.getApproxWritten()).isEqualTo(573);
}