From ad44db386bf722a26f45e5cd26a4290b6208e51d Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Mon, 5 Aug 2013 13:37:49 +0200 Subject: [PATCH] DATAMONGO-586 - Adjusted examples in reference documentation. Changed examples in reference documentation to reflect the new DSL style. --- .../core/aggregation/AggregationTests.java | 1 - src/docbkx/reference/mongodb.xml | 66 +++++++++++-------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java index 48d25d39c..e017bfb2d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java @@ -297,7 +297,6 @@ public class AggregationTests { .first("city").as("smallestCity") // .first("pop").as("smallestPop"), // project() // - // .and(previousOperation()).exclude() // .and("state").previousOperation() // .and("biggestCity").nested(bind("name", "biggestCity").and("population", "biggestPop")) // .and("smallestCity").nested(bind("name", "smallestCity").and("population", "smallestPop")), // diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index cd4b5b9ae..ef11b7235 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -2183,7 +2183,7 @@ GroupByResults<XObject> results = mongoTemplate.group(where("x").gt(0), The actual aggregate operation is executed by the aggregate method of the MongoTemplate which also takes the desired - output class as parameter. + output class as parameter. @@ -2206,7 +2206,7 @@ GroupByResults<XObject> results = mongoTemplate.group(where("x").gt(0), the result of an aggregate operation. It provides access to the raw aggreation result in the form of an DBObject, to the mapped objects and information which performed the - aggregation. + aggregation. @@ -2294,10 +2294,17 @@ List<OutputType> mappedResult = results.getMappedResults(); Group Aggregation Operators - add_to_set, first, last, max, min, avg, push, sum, + addToSet, first, last, max, min, avg, push, sum, (*count) + + Arithmetic Aggregation Operators + + add (*via plus), subtract (*via minus), multiply, divide, + mod + + Comparison Aggregation Operators @@ -2308,7 +2315,8 @@ List<OutputType> mappedResult = results.getMappedResults(); Note that the aggregation operations not listed here are currently - not supported by Spring Data MongoDB. + not supported by Spring Data MongoDB. Comparison aggregation operators + are expressed as Criteria expressions. *) The operation is mapped or added by Spring Data MongoDB. @@ -2325,26 +2333,27 @@ List<OutputType> mappedResult = results.getMappedResults(); In order to do this we first create a new aggregation via the newAggregation static factory method to which we pass a list of aggregation operations. These aggregate operations - form the aggregation pipeline of our Aggregation. - + form the aggregation pipeline of our + Aggregation. As a first step we select the "tags" field (which is an array of strings) from the input collection with the - project operation. + project operation. In a second step we use the unwind operation to generate a new document for each tag within the - "tags" array. + "tags" array. In the third step we use the group operation to define a group for each "tags"-value for which we aggregate the occurence count via the count aggregation operator and collect the result in a new field called - "n". + "n". As a forth step we select the field "n" and create an - alias for the id-field generated from the previous group operation with - the name "tag". + alias for the id-field generated from the previous group operation + (hence the call to previousOperation()) with the name + "tag". Finally as the fifth step we sort the resulting list of tags by their occurence count in descending order via the @@ -2353,7 +2362,7 @@ List<OutputType> mappedResult = results.getMappedResults(); In order to let MongoDB perform the acutal aggregation operation we call the aggregate Method on the MongoTemplate with the created Aggregation as an - argument. + argument. class TagCount { private String tag; @@ -2366,9 +2375,9 @@ import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; Aggregation agg = newAggregation( project("tags"), unwind("tags"), - group("tags").and("n").count(), + group("tags").count().as("n"), project("n").and("tag").previousOperation(), - sort(DESC, "n") + sort(DESC, "n") ); AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, "tags", TagCount.class); @@ -2379,7 +2388,7 @@ List<TagCount> tagCount = results.getMappedResults(); "tags" parameter to the aggregate Method. If the name of the input collection is not specified explicitly, it is derived from the input-class passed as first parameter to the - newAggreation Method. + newAggreation Method.
@@ -2412,7 +2421,7 @@ List<TagCount> tagCount = results.getMappedResults(); ascending order, such that the smallest city is at the top and the biggest city is at the bottom of the result. Note that the sorting on "state" and "city" is implicitly performed against the - group id fields which Spring Data MongoDB took care of. + group id fields which Spring Data MongoDB took care of. In the third step we use a group operation again to group the intermediate result by @@ -2430,7 +2439,7 @@ List<TagCount> tagCount = results.getMappedResults(); and(previousOperation()).exclude(). As we want to populate the nested City structures in our output-class accordingly we have to emit appropriate sub-documents with the nested - method. + method. Finally as the fifth step we sort the resulting list of StateStats by their state name in ascending order @@ -2462,18 +2471,17 @@ class ZipInfoStats { import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; … TypedAggregation<ZipInfo> aggregation = newAggregation(ZipInfo.class, - group("state", "city").and("pop").sum("population"), + group("state", "city").sum("population").as("pop"), sort(ASC, "pop", "state", "city"), group("state") - .and("biggestCity").last("city") - .and("biggestPop").last("pop") - .and("smallestCity").first("city") - .and("smallestPop").first("pop"), + .last("city").as("biggestCity") + .last("pop").as("biggestPop") + .first("city").as("smallestCity") + .first("pop").as("smallestPop"), project() - .and(previousOperation()).exclude() - .and("state").previousOperation() - .and("biggestCity").nested(bind("name", "biggestCity").and("population", "biggestPop")) - .and("smallestCity").nested(bind("name", "smallestCity").and("population", "smallestPop")), + .and("state").previousOperation() + .and("biggestCity").nested(bind("name", "biggestCity").and("population", "biggestPop")) + .and("smallestCity").nested(bind("name", "smallestCity").and("population", "smallestPop")), sort(ASC, "state") ); @@ -2522,9 +2530,9 @@ ZipInfoStats firstZipInfoStats = result.getMappedResults().get(0); import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; … TypedAggregation<ZipInfo> agg = newAggregation(ZipInfo.class, - group("state").and("totalPop").sum("population"), - sort(ASC, previousOperation(), "totalPop"), - match(where("totalPop").gte(10 * 1000 * 1000)) + group("state").sum("population").as("totalPop"), + sort(ASC, previousOperation(), "totalPop"), + match(where("totalPop").gte(10 * 1000 * 1000)) ); AggregationResults<StateStats> result = mongoTemplate.aggregate(agg, StateStats.class);