From 2bd78e0bf03b911369839070114c84153ae36227 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 17 Oct 2013 13:01:24 +0200 Subject: [PATCH] DATAMONGO-774 - Added usage examples for SpEL expressions in projections to the reference documentation. Original pull request: #81. --- src/docbkx/reference/mongodb.xml | 153 ++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-) diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 32215fba3..abe5acdbb 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -1118,7 +1118,7 @@ class SampleMongoConfiguration extends AbstractMongoConfiguration { AbstractMongoConfiguration class and override the bean definition of the MappingMongoConverter where we configure our - custom MongoTypeMapper. + custom MongoTypeMapper. @@ -2391,6 +2391,83 @@ List<OutputType> mappedResult = results.getMappedResults(); *) The operation is mapped or added by Spring Data MongoDB. +
+ Projection Expressions + + Projection expressions are used to define the fields that are the + outcome of a particular aggregation step. Projection expressions can be + defined via the project method of the + Aggregate class. + + + Projection expression examples + + project("name", "netPrice") // will generate {$project: {name: 1, netPrice: 1}} +project().and("foo").as("bar") // will generate {$project: {bar: $foo}} +project("a","b").and("foo").as("bar") // will generate {$project: {a: 1, b: 1, bar: $foo}} + + Note that more examples for project operations can be found in + the AggregationTests class. + + + Note that further details regarding the projection expressions can + be found in the corresponding + section of the MongoDB Aggregation Framework reference + documentation. + +
+ Spring Expresison Support in Projection Expressions + + Since Version 1.4.0 we support the use of SpEL expression in + Projection expressions via the andExpression + method of the ProjectionOperation class. This + allows a developer to define the desired expression as a SpEL + expression string which is then translated to a corresponding MongoDB + projection expression part on query execution. + + Note that one can find more usage examples for supported SpEL + expression constructs in + SpelExpressionToMongoExpressionTransformerUnitTests. + + This makes it much easier to express complex + calculations. + + + Complex calulations with SpEL expressions + + The following SpEL expression: + + 1 + (q + 1) / (q - 1) + + will be translated into the following projection expression + part: + + { + "$add":[ + 1, + { + "$divide":[ + { + "$add":[ + "$q", + 1 + ] + }, + { + "$subtract":[ + "$q", + 1 + ] + } + ] + } + ] +} + +
+
+
Aggregation Framework Examples @@ -2659,8 +2736,8 @@ List<StateStats> stateStatsList = result.getMappedResults(); Aggregation Framework Example 4 - This example demonstrates the use of arithmetic operations in - the projection operation. + This example demonstrates the use of simple arithmetic + operations in the projection operation. class Product { String id; @@ -2687,6 +2764,76 @@ List<DBObject> resultList = result.getMappedResults(); Note that we derive the name of the input-collection from the Product-class passed as first parameter to the newAggregation-Method. + + + Aggregation Framework Example 5 + + This example demonstrates the use of simple arithmetic + operations derived from SpEL Expressions in the projection + operation. + + class Product { + String id; + String name; + double netPrice; + int spaceUnits; +} + + import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; + +TypedAggregation<Product> 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<DBObject> result = mongoTemplate.aggregate(agg, DBObject.class); +List<DBObject> resultList = result.getMappedResults(); + + + + Aggregation Framework Example 6 + + This example demonstrates the use of complex arithmetic + operations derived from SpEL Expressions in the projection + operation. + + Note: The additional parameters passed to the + addExpression Method can be referenced via + indexer expressions according to their position. In this example we + reference the parameter shippingCosts which is the + first parameter of the parameters array via [0]. External + parameter expressions are replaced with their respective values when + the SpEL expression is transformed into a MongoDB 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<Product> agg = newAggregation(Product.class, + project("name", "netPrice") + .andExpression("(netPrice * (1-discountRate) + [0]) * (1+taxRate)", shippingCosts).as("salesPrice") +); + +AggregationResults<DBObject> result = mongoTemplate.aggregate(agg, DBObject.class); +List<DBObject> resultList = result.getMappedResults(); + + + Note that we can also refer to other fields of the document within + the SpEL expression.