Example usage for org.springframework.data.mongodb.core.aggregation Order getId

List of usage examples for org.springframework.data.mongodb.core.aggregation Order getId

Introduction

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

Prototype

public String getId() 

Source Link

Usage

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

/**
 * The implementation uses the MongoDB aggregation framework support Spring Data provides as well as SpEL expressions
 * to define arithmetical expressions. Note how we work with property names only and don't have to mitigate the nested
 * {@code $_id} fields MongoDB usually requires.
 * //from  w w w  . j  a va 2 s.co  m
 * @see example.springdata.mongodb.aggregation.OrderRepositoryCustom#getInvoiceFor(example.springdata.mongodb.aggregation.Order)
 */
@Override
public Invoice getInvoiceFor(Order order) {

    AggregationResults<Invoice> results = operations.aggregate(newAggregation(Order.class, //
            match(where("id").is(order.getId())), //
            unwind("items"), //
            project("id", "customerId", "items") //
                    .andExpression("'$items.price' * '$items.quantity'").as("lineTotal"), //
            group("id") //
                    .sum("lineTotal").as("netAmount") //
                    .addToSet("items").as("items"), //
            project("id", "items", "netAmount") //
                    .and("orderId").previousOperation() //
                    .andExpression("netAmount * [0]", taxRate).as("taxAmount") //
                    .andExpression("netAmount * (1 + [0])", taxRate).as("totalAmount") //
    ), Invoice.class);

    return results.getUniqueMappedResult();
}