@Retention(value=RUNTIME) @Target(value={METHOD,ANNOTATION_TYPE}) @Documented public @interface Aggregation
Aggregation
annotation can be used to annotate a Repository
query method so that it runs the pipeline()
on invocation.
Pipeline stages are mapped against the Repository
domain type to consider
field
mappings and may contain simple placeholders
?0
as well as SpelExpressions
.
Query method Sort
and Pageable
arguments are applied at the end of the pipeline or can be defined manually as part of it.@AliasFor(value="pipeline") public abstract String[] value
pipeline()
. Defines the aggregation pipeline to apply.pipeline()
@AliasFor(value="value") public abstract String[] pipeline
// aggregation resulting in collection with single value @Aggregation("{ '$project': { '_id' : '$lastname' } }") ListfindAllLastnames(); // aggregation with parameter replacement @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }") List groupByLastnameAnd(String property); // aggregation with sort in pipeline @Aggregation(pipeline = {"{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }", "{ '$sort' : { 'lastname' : -1 } }"}) List groupByLastnameAnd(String property); // Sort parameter is used for sorting results @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }") List groupByLastnameAnd(String property, Sort sort); // Pageable parameter used for sort, skip and limit @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }") List groupByLastnameAnd(String property, Pageable page); // Single value result aggregation. @Aggregation("{ '$group' : { '_id' : null, 'total' : { $sum: '$age' } } }") Long sumAge(); // Single value wrapped in container object @Aggregation("{ '$group' : { '_id' : null, 'total' : { $sum: '$age' } } }) SumAge sumAgeAndReturnAggregationResultWrapperWithConcreteType(); // Raw aggregation result @Aggregation("{ '$group' : { '_id' : null, 'total' : { $sum: '$age' } } }) AggregationResults<org.bson.Document>> sumAgeAndReturnAggregationResultWrapper();
public abstract String collation
// Fixed value @Aggregation(pipeline = "...", collation = "en_US") ListfindAllByFixedCollation(); // Fixed value as Document @Aggregation(pipeline = "...", collation = "{ 'locale' : 'en_US' }") List findAllByFixedJsonCollation(); // Dynamic value as String @Aggregation(pipeline = "...", collation = "?0") List findAllByDynamicCollation(String collation); // Dynamic value as Document @Aggregation(pipeline = "...", collation = "{ 'locale' : ?0 }") List findAllByDynamicJsonCollation(String collation); // SpEL expression @Aggregation(pipeline = "...", collation = "?#{[0]}") List findAllByDynamicSpElCollation(String collation);
String
by default.Copyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.