Annotation Interface Aggregation


@Retention(RUNTIME) @Target({METHOD,ANNOTATION_TYPE}) @Documented public @interface Aggregation
The 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.
Since:
2.2
Author:
Christoph Strobl
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Defines the collation to apply when executing the aggregation.
    Defines the aggregation pipeline to apply.
    Alias for pipeline().
  • Element Details

    • value

      @AliasFor("pipeline") String[] value
      Alias for pipeline(). Defines the aggregation pipeline to apply.
      Returns:
      an empty array by default.
      See Also:
      Default:
      {}
    • pipeline

      @AliasFor("value") String[] pipeline
      Defines the aggregation pipeline to apply.
      
       // aggregation resulting in collection with single value
       @Aggregation("{ '$project': { '_id' : '$lastname' } }")
       List<String> findAllLastnames();
      
       // aggregation with parameter replacement
       @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }")
       List<PersonAggregate> groupByLastnameAnd(String property);
      
       // aggregation with sort in pipeline
       @Aggregation(pipeline = {"{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }", "{ '$sort' : { 'lastname' : -1 } }"})
       List<PersonAggregate> groupByLastnameAnd(String property);
      
       // Sort parameter is used for sorting results
       @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }")
       List<PersonAggregate> groupByLastnameAnd(String property, Sort sort);
      
       // Pageable parameter used for sort, skip and limit
       @Aggregation("{ '$group': { '_id' : '$lastname', names : { $addToSet : '$?0' } } }")
       List<PersonAggregate> 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();
       
      Returns:
      an empty array by default.
      Default:
      {}
    • collation

      Defines the collation to apply when executing the aggregation.
       // Fixed value
       @Aggregation(pipeline = "...", collation = "en_US")
       List<Entry> findAllByFixedCollation();
      
       // Fixed value as Document
       @Aggregation(pipeline = "...", collation = "{ 'locale' :  'en_US' }")
       List<Entry> findAllByFixedJsonCollation();
      
       // Dynamic value as String
       @Aggregation(pipeline = "...", collation = "?0")
       List<Entry> findAllByDynamicCollation(String collation);
      
       // Dynamic value as Document
       @Aggregation(pipeline = "...", collation = "{ 'locale' :  ?0 }")
       List<Entry> findAllByDynamicJsonCollation(String collation);
      
       // SpEL expression
       @Aggregation(pipeline = "...", collation = "?#{[0]}")
       List<Entry> findAllByDynamicSpElCollation(String collation);
       
      Returns:
      an empty String by default.
      Default:
      ""