Example usage for org.springframework.data.mongodb.core.aggregation ArrayOperators arrayOf

List of usage examples for org.springframework.data.mongodb.core.aggregation ArrayOperators arrayOf

Introduction

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

Prototype

public static ArrayOperatorFactory arrayOf(Collection<?> values) 

Source Link

Document

Take the given Collection values 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 w  w  w . j ava2 s.  c  o  m
@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);
}