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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

please define productname in your docbook file!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]}]}]}Example 5.16 Complex calulations with SpEL expressionsHave a look at an example in more context in Example 5.21, “Aggregation Framework Example 5” andExample 5.22, “Aggregation Framework Example 6”. You can find more usage examples for supportedSpEL expression constructs in SpelExpressionTransformerUnitTests.Aggregation Framework ExamplesThe follwing examples demonstrate the usage patterns for the <strong>MongoDB</strong> Aggregation Framework with<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong>.In this introductory example we want to aggregate a list of tags to get the occurence count of a particulartag from a <strong>MongoDB</strong> collection called "tags" sorted by the occurence count in descending order.This example demonstrates the usage of grouping, sorting, projections (selection) and unwinding (resultsplitting).class TagCount {String tag;int n;}import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;Aggregation agg = newAggregation(project("tags"),unwind("tags"),group("tags").count().as("n"),project("n").and("tag").previousOperation(),sort(DESC, "n"));AggregationResults results = mongoTemplate.aggregate(agg, "tags",TagCount.class);List tagCount = results.getMappedResults();Example 5.17 Aggregation Framework Example 1• In order to do this we first create a new aggregation via the newAggregation static factory methodto which we pass a list of aggregation operations. These aggregate operations define the aggregationpipeline of our Aggregation.• As a second step we select the "tags" field (which is an array of strings) from the input collectionwith the project operation.• In a third step we use the unwind operation to generate a new document for each tag within the"tags" array.1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 56

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

Saved successfully!

Ooh no, something went wrong!