13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

please define productname in your docbook file!Note that we derive the name of the input-collection from the ZipInfo-class passed as first parameterto the newAggregation-Method.This example demonstrates the use of simple arithmetic operations in the projection operation.class Product {String id;String name;double netPrice;int spaceUnits;}import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;TypedAggregation agg = newAggregation(Product.class,project("name", "netPrice").and("netPrice").plus(1).as("netPricePlus1").and("netPrice").minus(1).as("netPriceMinus1").and("netPrice").multiply(1.19).as("grossPrice").and("netPrice").divide(2).as("netPriceDiv2").and("spaceUnits").mod(2).as("spaceUnitsMod2"));AggregationResults result = mongoTemplate.aggregate(agg, DBObject.class);List resultList = result.getMappedResults();Example 5.20 Aggregation Framework Example 4Note that we derive the name of the input-collection from the Product-class passed as first parameterto the newAggregation-Method.This example demonstrates the use of simple arithmetic operations derived from SpEL Expressions inthe projection operation.class Product {String id;String name;double netPrice;int spaceUnits;}import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;TypedAggregation agg = newAggregation(Product.class,project("name", "netPrice").andExpression("netPrice + 1").as("netPricePlus1").andExpression("netPrice - 1").as("netPriceMinus1").andExpression("netPrice / 2").as("netPriceDiv2").andExpression("netPrice * 1.19").as("grossPrice").andExpression("spaceUnits % 2").as("spaceUnitsMod2").andExpression("(netPrice * 0.8 + 1.2) *1.19").as("grossPriceIncludingDiscountAndCharge"));AggregationResults result = mongoTemplate.aggregate(agg, DBObject.class);List resultList = result.getMappedResults();Example 5.21 Aggregation Framework Example 51.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 60

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!