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!This example demonstrates the use of complex arithmetic operations derived from SpEL Expressionsin the projection operation.Note: The additional parameters passed to the addExpression Method can be referenced via indexerexpressions according to their position. In this example we reference the parameter shippingCostswhich is the first parameter of the parameters array via [0]. External parameter expressions arereplaced with their respective values when the SpEL expression is transformed into a <strong>MongoDB</strong>aggregation framework expression.class Product {String id;String name;double netPrice;int spaceUnits;}import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;double shippingCosts = 1.2;TypedAggregation agg = newAggregation(Product.class,project("name", "netPrice").andExpression("(netPrice * (1-discountRate) + [0]) * (1+taxRate)",shippingCosts).as("salesPrice"));AggregationResults result = mongoTemplate.aggregate(agg, DBObject.class);List resultList = result.getMappedResults();Example 5.22 Aggregation Framework Example 6Note that we can also refer to other fields of the document within the SpEL expression.5.10 Overriding default mapping with custom convertersIn order to have more fine grained control over the mapping process you can register <strong>Spring</strong> converterswith the MongoConverter implementations such as the MappingMongoConverter.The MappingMongoConverter checks to see if there are any <strong>Spring</strong> converters that can handle aspecific class before attempting to map the object itself. To 'hijack' the normal mapping strategies ofthe MappingMongoConverter, perhaps for increased performance or other custom mapping needs,you first need to create an implementation of the <strong>Spring</strong> Converter interface and then register it withthe MappingConverter.NoteFor more information on the <strong>Spring</strong> type conversion service see the reference docs here.Saving using a registered <strong>Spring</strong> ConverterAn example implementation of the Converter that converts from a Person object to acom.mongodb.DBObject is shown below1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 61

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

Saved successfully!

Ooh no, something went wrong!