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!Basic ConceptsThe Aggregation Framework support in <strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> is based on the following key abstractionsAggregation, AggregationOperation and AggregationResults.• AggregationAn Aggregation represents a <strong>MongoDB</strong> aggregate operation and holds the description ofthe aggregation pipline instructions. Aggregations are created by inoking the appropriatenewAggregation(…) static factory Method of the Aggregation class which takes the list ofAggregateOperation as a parameter next to the optional input class.The actual aggregate operation is executed by the aggregate method of the MongoTemplate whichalso takes the desired output class as parameter.• AggregationOperationAn AggregationOperation represents a <strong>MongoDB</strong> aggregation pipeline operation and describesthe processing that should be performed in this aggregation step. Although one could manually createan AggregationOperation the recommended way to construct an AggregateOperation is touse the static factory methods provided by the Aggregate class.• AggregationResultsAggregationResults is the container for the result of an aggregate operation. It provides accessto the raw aggreation result in the form of an DBObject, to the mapped objects and information whichperformed the aggregation.The canonical example for using the <strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> support for the <strong>MongoDB</strong> AggregationFramework looks as follows:import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;Aggregation agg = newAggregation(pipelineOP1(),pipelineOP2(),pipelineOPn());AggregationResults results =mongoTemplate.aggregate(agg, "INPUT_COLLECTION_NAME", OutputType.class);List mappedResult = results.getMappedResults();Note that if you provide an input class as the first parameter to the newAggregation method theMongoTemplate will derive the name of the input collection from this class. Otherwise if you don't notspecify an input class you must provide the name of the input collection explicitly. If an input-class andan explicity input-collection is provided the latter takes precedence.Supported Aggregation OperationsThe <strong>MongoDB</strong> Aggregation Framework provides the following types of Aggregation Operations:• Pipeline Aggregation Operators• Group Aggregation Operators1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 54

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

Saved successfully!

Ooh no, something went wrong!