The Aggregation class in Spring Data MongoDB is the entry point for building MongoDB aggregation pipelines in Java. It provides static factory methods for stages such as match, group, project, sort, lookup and unwind, so you can express grouping, filtering and reshaping of data with type-safe code and run it through MongoTemplate.
Aggregation pipelines run on the database server, which is far more efficient than loading documents and processing them in Java.
A MongoDB aggregation pipeline is an ordered list of stages. Each stage transforms the documents it receives and passes the result to the next. In Spring Data you create the pipeline with Aggregation.newAggregation(...), pass the stages, and execute it with MongoTemplate.aggregate().
| Spring Data method | MongoDB stage | Purpose |
|---|---|---|
Aggregation.match() | $match | Filter documents |
Aggregation.group() | $group | Group and compute sums, averages, counts |
Aggregation.project() | $project | Include, exclude or compute fields |
Aggregation.sort() | $sort | Order results |
Aggregation.limit() / skip() | $limit / $skip | Paging |
Aggregation.unwind() | $unwind | Flatten arrays |
Aggregation.lookup() | $lookup | Join another collection |
This example calculates total and average order value per customer, for completed orders only, and returns the top ten.
group("customerId") uses the group key as _id, and previousOperation() maps that _id back to a customerId field in the output. The second argument of aggregate() is the collection name, and the third is the type the results are mapped to. Passing an entity class instead of a name lets Spring map field names and types for you.
lookup(from, localField, foreignField, as) produces an array, and unwind turns it into one document per element. Because $lookup can be expensive on large collections, match and limit early and index the foreign field.
If you pass an entity type to newAggregation(Order.class, ...), Spring checks field references against the entity and maps Java property names to the stored field names, including @Field renames. Use TypedAggregation when you want this check at the pipeline level.
match and sort first so they can use indexes. A $match placed after a $group cannot.project to reduce document size early when you only need a few fields.Aggregation.newAggregation(...).withOptions(AggregationOptions.builder().allowDiskUse(true).build()).Query and repositories. Reserve aggregations for grouping, joins and computed fields.@Aggregation(pipeline = {...}) to run a pipeline written as JSON strings.Use Query to find and filter documents. Use Aggregation when you need grouping, calculated fields, joins or multi-step transformations.
Pass Document.class as the output type, and read getMappedResults() to get Document objects.
Add skip() and limit() stages after sort(). For total counts, run a second aggregation with a count() stage or use facet().
Yes. ReactiveMongoTemplate.aggregate() accepts the same Aggregation objects and returns a Flux.
The Aggregation class lets you build MongoDB aggregation pipelines in Java and run them on the server through MongoTemplate. Compose match, group, project, sort and lookup stages, map results to records, and place filters early to keep pipelines fast.