Class Aggregation
java.lang.Object
org.springframework.data.mongodb.core.aggregation.Aggregation
- Direct Known Subclasses:
AggregationUpdate,TypedAggregation
An
Aggregation is a representation of a list of aggregation steps to be performed by the MongoDB Aggregation
Framework.- Since:
- 1.3
- Author:
- Tobias Trelle, Thomas Darimont, Oliver Gierke, Mark Paluch, Alessio Fachechi, Christoph Strobl, Nikolay Bogdanov, Gustavo de Geus, Jérôme Guyon, Sangyong Choi
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringReferences the start of the field path being processed in the aggregation pipeline stage.static final AggregationOperationContextstatic final AggregationOptionsprotected final AggregationPipelinestatic final StringA variable to conditionally exclude a field.static final StringReferences the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAggregation(List<AggregationOperation> aggregationOperations) Creates a newAggregationfrom the givenAggregationOperations.protectedAggregation(List<AggregationOperation> aggregationOperations, AggregationOptions options) Creates a newAggregationfrom the givenAggregationOperations.protectedAggregation(AggregationOperation... aggregationOperations) Creates a newAggregationfrom the givenAggregationOperations. -
Method Summary
Modifier and TypeMethodDescriptionObtain anbuilderinstance to create a newAddFieldsOperation.protected static List<AggregationOperation>asAggregationList(AggregationOperation... aggregationOperations) static FieldsCreates a newFieldsinstance from the given field name and target reference.static BucketOperationCreates a newBucketOperationgiven groupByField.static BucketOperationbucket(AggregationExpression groupByExpression) Creates a newBucketOperationgivengroup-by expression.static BucketAutoOperationbucketAuto(String groupByField, int buckets) Creates a newBucketAutoOperationgiven groupByField.static BucketAutoOperationbucketAuto(AggregationExpression groupByExpression, int buckets) Creates a newBucketAutoOperationgivengroup-by expression.count()Creates a newCountOperation.CountOperationBuilder.static FacetOperationfacet()Creates a newFacetOperation.facet(AggregationOperation... aggregationOperations) Creates a newFacetOperation.FacetOperationBuildergivenAggregation.static FieldsCreates a newFieldsinstance for the given field names.static GeoNearOperationGet theAggregationOptions.graphLookup(String fromCollection) Creates a newGraphLookupOperation.GraphLookupOperationFromBuilderto construct aGraphLookupOperationgiven fromCollection.static GroupOperationCreates a newGroupOperationfor the given fields.static GroupOperationCreates a newGroupOperationfor the givenFields.static LimitOperationlimit(long maxElements) Creates a newLimitOperationlimiting the result to the given number of elements.lookup()Entrypoint for creating$lookupusing a fluent builder API.static LookupOperationCreates a newLookupOperation.static LookupOperationCreates a newLookupOperationfor the givenFields.static MatchOperationmatch(AggregationExpression expression) Creates a newMatchOperationusing the givenAggregationExpression.static MatchOperationCreates a newMatchOperationusing the givenCriteria.static MatchOperationmatch(CriteriaDefinition criteria) Creates a newMatchOperationusing the givenCriteriaDefinition.merge()Obtain abuilderinstance to create a newMergeOperation.static <T> TypedAggregation<T>newAggregation(Class<T> type, List<? extends AggregationOperation> operations) Creates a newTypedAggregationfor the given type andAggregationOperations.static <T> TypedAggregation<T>newAggregation(Class<T> type, AggregationOperation... operations) Creates a newTypedAggregationfor the given type andAggregationOperations.static AggregationnewAggregation(List<? extends AggregationOperation> operations) Creates a newAggregationfrom the givenAggregationOperations.static AggregationnewAggregation(AggregationOperation... operations) Creates a newAggregationfrom the givenAggregationOperations.static AggregationOptions.BuilderReturns a newAggregationOptions.Builder.static AggregationUpdatenewUpdate(AggregationOperation... operations) Creates a newAggregationUpdatefrom the givenAggregationOperations.static OutOperationCreates a newOutOperationusing the given collection name.static StringA pointer to the previousAggregationOperation.static ProjectionOperationCreates a newProjectionOperationincluding all top level fields of the given givenClass.static ProjectionOperationCreates a newProjectionOperationincluding the given fields.static ProjectionOperationCreates a newProjectionOperationincluding the givenFields.static RedactOperationredact(AggregationExpression condition) Creates a newRedactOperationthat can restrict the content of a document based on information stored within the document itself.Factory method to create a newReplaceRootOperation.ReplaceRootDocumentOperationBuilderto configure aReplaceRootOperation.static ReplaceRootOperationreplaceRoot(String fieldName) Factory method to create a newReplaceRootOperationfor the field with the given name.static ReplaceRootOperationreplaceRoot(AggregationExpression aggregationExpression) Factory method to create a newReplaceRootOperationfor the field with the givenAggregationExpression.static SampleOperationsample(long sampleSize) Creates a newSampleOperationto select the specified number of documents from its input randomly.static SkipOperationskip(long elementsToSkip) Creates a newSkipOperationskipping the given number of elements.static SortOperationFactory method to create a newSortOperationfor the givenSort.static SortOperationsort(Sort.Direction direction, String... fields) static SortByCountOperationsortByCount(String field) Creates a newSortByCountOperationgiven groupByField.static SortByCountOperationsortByCount(AggregationExpression groupAndSortExpression) Creates a newSortByCountOperationgivengroup and sort expression.static AggregationOperationCreates a newAggregationOperationtaking the givenjson valueas is.static AggregationOperationstage(org.bson.conversions.Bson aggregationOperation) Creates a newAggregationOperationtaking the givenbson valueas is.org.bson.DocumenttoDocument(String inputCollectionName, AggregationOperationContext rootContext) Converts thisAggregationspecification to aDocument.List<org.bson.Document>toPipeline(AggregationOperationContext rootContext) toString()static UnwindOperationFactory method to create a newUnwindOperationfor the field with the given name.static UnwindOperationFactory method to create a newUnwindOperationfor the field with the given name andpreserveNullAndEmptyArrays.static UnwindOperationFactory method to create a newUnwindOperationfor the field with the given name including the name of a new field to hold the array index of the element asarrayIndex.static UnwindOperationFactory method to create a newUnwindOperationfor the field with the given name, including the name of a new field to hold the array index of the element asarrayIndexusingpreserveNullAndEmptyArrays.vectorSearch(String indexName) Creates a newVectorSearchOperationby starting from theindexNameto use.withOptions(AggregationOptions options) Returns a copy of thisAggregationwith the givenAggregationOptionsset.
-
Field Details
-
ROOT
References the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage. -
CURRENT
References the start of the field path being processed in the aggregation pipeline stage. Unless documented otherwise, all stages start with CURRENT the same as ROOT. -
REMOVE
A variable to conditionally exclude a field. In a$projection, a field set to the variable REMOVE is excluded from the output.db.books.aggregate( [ { $project: { title: 1, "author.first": 1, "author.last" : 1, "author.middle": { $cond: { if: { $eq: [ "", "$author.middle" ] }, then: "$$REMOVE", else: "$author.middle" } } } } ] ) -
DEFAULT_CONTEXT
-
DEFAULT_OPTIONS
-
pipeline
-
-
Constructor Details
-
Aggregation
Creates a newAggregationfrom the givenAggregationOperations.- Parameters:
aggregationOperations- must not be null or empty.
-
Aggregation
Creates a newAggregationfrom the givenAggregationOperations.- Parameters:
aggregationOperations- must not be null or empty.
-
Aggregation
Creates a newAggregationfrom the givenAggregationOperations.- Parameters:
aggregationOperations- must not be null.options- must not be null or empty.
-
-
Method Details
-
newAggregation
Creates a newAggregationfrom the givenAggregationOperations.- Parameters:
operations- must not be null or empty.
-
newAggregation
Creates a newAggregationfrom the givenAggregationOperations.- Parameters:
operations- must not be null or empty.
-
newUpdate
Creates a newAggregationUpdatefrom the givenAggregationOperations.- Parameters:
operations- can be empty but must not be null.- Returns:
- new instance of
AggregationUpdate. - Since:
- 3.0
-
withOptions
Returns a copy of thisAggregationwith the givenAggregationOptionsset. Note that options are supported in MongoDB version 2.6+.- Parameters:
options- must not be null.- Returns:
- new instance of
Aggregation. - Since:
- 1.6
-
newAggregation
public static <T> TypedAggregation<T> newAggregation(Class<T> type, List<? extends AggregationOperation> operations) Creates a newTypedAggregationfor the given type andAggregationOperations.- Parameters:
type- must not be null.operations- must not be null or empty.
-
newAggregation
public static <T> TypedAggregation<T> newAggregation(Class<T> type, AggregationOperation... operations) Creates a newTypedAggregationfor the given type andAggregationOperations.- Parameters:
type- must not be null.operations- must not be null or empty.
-
asAggregationList
protected static List<AggregationOperation> asAggregationList(AggregationOperation... aggregationOperations) - Parameters:
aggregationOperations- must not be null or empty.- Returns:
-
getOptions
Get theAggregationOptions.- Returns:
- never null.
- Since:
- 2.1
-
previousOperation
A pointer to the previousAggregationOperation.- Returns:
-
addFields
Obtain anbuilderinstance to create a newAddFieldsOperation.
Starting in version 4.2, MongoDB adds a new aggregation pipeline stage$setthat is an alias for$addFields.- Returns:
- new instance of
AddFieldsOperation.AddFieldsOperationBuilder. - Since:
- 3.0
- See Also:
-
stage
Creates a newAggregationOperationtaking the givenbson valueas is.
Aggregation.stage(Aggregates.search(exists(fieldPath("..."))));Field mapping against a potential domain type or previous aggregation stages will not happen.- Parameters:
aggregationOperation- the must not be null.- Returns:
- new instance of
AggregationOperation. - Since:
- 4.0
-
stage
Creates a newAggregationOperationtaking the givenjson valueas is.
Aggregation.stage("{ $search : { near : { path : 'released' , origin : ... } } }");Field mapping against a potential domain type or previous aggregation stages will not happen.- Parameters:
json- the JSON representation of the pipeline stage. Must not be null.- Returns:
- new instance of
AggregationOperation. - Since:
- 4.0
-
project
Creates a newProjectionOperationincluding the given fields.- Parameters:
fields- must not be null.- Returns:
- new instance of
ProjectionOperation.
-
project
Creates a newProjectionOperationincluding the givenFields.- Parameters:
fields- must not be null.- Returns:
- new instance of
ProjectionOperation.
-
project
Creates a newProjectionOperationincluding all top level fields of the given givenClass.- Parameters:
type- must not be null.- Returns:
- new instance of
ProjectionOperation. - Since:
- 2.2
-
unwind
Factory method to create a newUnwindOperationfor the field with the given name.- Parameters:
field- must not be null or empty.- Returns:
- new instance of
UnwindOperation.
-
replaceRoot
Factory method to create a newReplaceRootOperationfor the field with the given name.- Parameters:
fieldName- must not be null or empty.- Returns:
- new instance of
ReplaceRootOperation. - Since:
- 1.10
-
replaceRoot
Factory method to create a newReplaceRootOperationfor the field with the givenAggregationExpression.- Parameters:
aggregationExpression- must not be null.- Returns:
- new instance of
ReplaceRootOperation. - Since:
- 1.10
-
replaceRoot
Factory method to create a newReplaceRootOperation.ReplaceRootDocumentOperationBuilderto configure aReplaceRootOperation.- Returns:
- the ReplaceRootDocumentOperationBuilder.
- Since:
- 1.10
-
unwind
Factory method to create a newUnwindOperationfor the field with the given name andpreserveNullAndEmptyArrays. Note that extended unwind is supported in MongoDB version 3.2+.- Parameters:
field- must not be null or empty.preserveNullAndEmptyArrays- true to output the document if path is null, missing or array is empty.- Returns:
- new
UnwindOperation - Since:
- 1.10
-
unwind
Factory method to create a newUnwindOperationfor the field with the given name including the name of a new field to hold the array index of the element asarrayIndex. Note that extended unwind is supported in MongoDB version 3.2+.- Parameters:
field- must not be null or empty.arrayIndex- must not be null or empty.- Returns:
- new
UnwindOperation - Since:
- 1.10
-
unwind
public static UnwindOperation unwind(String field, String arrayIndex, boolean preserveNullAndEmptyArrays) Factory method to create a newUnwindOperationfor the field with the given name, including the name of a new field to hold the array index of the element asarrayIndexusingpreserveNullAndEmptyArrays. Note that extended unwind is supported in MongoDB version 3.2+.- Parameters:
field- must not be null or empty.arrayIndex- must not be null or empty.preserveNullAndEmptyArrays- true to output the document if path is null, missing or array is empty.- Returns:
- new
UnwindOperation - Since:
- 1.10
-
group
Creates a newGroupOperationfor the given fields.- Parameters:
fields- must not be null.- Returns:
- new instance of
GroupOperation.
-
group
Creates a newGroupOperationfor the givenFields.- Parameters:
fields- must not be null.- Returns:
-
graphLookup
Creates a newGraphLookupOperation.GraphLookupOperationFromBuilderto construct aGraphLookupOperationgiven fromCollection.- Parameters:
fromCollection- must not be null or empty.- Returns:
- new instance of
GraphLookupOperation.StartWithBuilderfor creating aGraphLookupOperation. - Since:
- 1.10
-
vectorSearch
Creates a newVectorSearchOperationby starting from theindexNameto use.- Parameters:
indexName- must not be null or empty.- Returns:
- new instance of
VectorSearchOperation.PathContributor. - Since:
- 4.5
-
sort
Factory method to create a newSortOperationfor the givenSort.- Parameters:
sort- must not be null.- Returns:
- new instance of
SortOperation.
-
sort
- Parameters:
direction- must not be null.fields- must not be null.- Returns:
- new instance of
SortOperation.
-
sortByCount
Creates a newSortByCountOperationgiven groupByField.- Parameters:
field- must not be null or empty.- Returns:
- new instance of
SortByCountOperation. - Since:
- 2.1
-
sortByCount
Creates a newSortByCountOperationgivengroup and sort expression.- Parameters:
groupAndSortExpression- must not be null.- Returns:
- new instance of
SortByCountOperation. - Since:
- 2.1
-
skip
Creates a newSkipOperationskipping the given number of elements.- Parameters:
elementsToSkip- must not be less than zero.- Returns:
- new instance of
SkipOperation.
-
limit
Creates a newLimitOperationlimiting the result to the given number of elements.- Parameters:
maxElements- must not be less than zero.- Returns:
- new instance of
LimitOperation.
-
sample
Creates a newSampleOperationto select the specified number of documents from its input randomly.- Parameters:
sampleSize- must not be less than zero.- Returns:
- new instance of
SampleOperation. - Since:
- 2.0
-
match
Creates a newMatchOperationusing the givenCriteria.- Parameters:
criteria- must not be null.- Returns:
- new instance of
MatchOperation.
-
match
Creates a newMatchOperationusing the givenCriteriaDefinition.- Parameters:
criteria- must not be null.- Returns:
- new instance of
MatchOperation. - Since:
- 1.10
-
match
Creates a newMatchOperationusing the givenAggregationExpression.- Parameters:
expression- must not be null.- Returns:
- new instance of
MatchOperation. - Since:
- 3.3
-
geoNear
Creates a newGeoNearOperationinstance from the givenNearQueryand thedistanceField. ThedistanceFielddefines output field that contains the calculated distance.- Parameters:
query- must not be null.distanceField- must not be null or empty.- Returns:
- new instance of
GeoNearOperation. - Since:
- 1.7
-
merge
Obtain abuilderinstance to create a newMergeOperation.- Returns:
- new instance of
MergeOperation.MergeOperationBuilder. - Since:
- 3.0
- See Also:
-
out
Creates a newOutOperationusing the given collection name. This operation must be the last operation in the pipeline.- Parameters:
outCollectionName- collection name to export aggregation results. TheOutOperationcreates a new collection in the current database if one does not already exist. The collection is not visible until the aggregation completes. If the aggregation fails, MongoDB does not create the collection. Must not be null.- Returns:
- new instance of
OutOperation.
-
bucket
Creates a newBucketOperationgiven groupByField.- Parameters:
groupByField- must not be null or empty.- Returns:
- new instance of
BucketOperation. - Since:
- 1.10
-
bucket
Creates a newBucketOperationgivengroup-by expression.- Parameters:
groupByExpression- must not be null.- Returns:
- new instance of
BucketOperation. - Since:
- 1.10
-
bucketAuto
Creates a newBucketAutoOperationgiven groupByField.- Parameters:
groupByField- must not be null or empty.buckets- number of buckets, must be a positive integer.- Returns:
- new instance of
BucketAutoOperation. - Since:
- 1.10
-
bucketAuto
Creates a newBucketAutoOperationgivengroup-by expression.- Parameters:
groupByExpression- must not be null.buckets- number of buckets, must be a positive integer.- Returns:
- new instance of
BucketAutoOperation. - Since:
- 1.10
-
facet
Creates a newFacetOperation.- Returns:
- new instance of
FacetOperation. - Since:
- 1.10
-
facet
public static FacetOperation.FacetOperationBuilder facet(AggregationOperation... aggregationOperations) Creates a newFacetOperation.FacetOperationBuildergivenAggregation.- Parameters:
aggregationOperations- the sub-pipeline, must not be null.- Returns:
- new instance of
FacetOperation. - Since:
- 1.10
-
lookup
public static LookupOperation lookup(String from, String localField, String foreignField, String as) Creates a newLookupOperation.- Parameters:
from- must not be null.localField- must not be null.foreignField- must not be null.as- must not be null.- Returns:
- never null.
- Since:
- 1.9
-
lookup
Creates a newLookupOperationfor the givenFields.- Parameters:
from- must not be null.localField- must not be null.foreignField- must not be null.as- must not be null.- Returns:
- never null.
- Since:
- 1.9
-
lookup
Entrypoint for creating$lookupusing a fluent builder API.Aggregation.lookup().from("restaurants").localField("restaurant_name").foreignField("name") .let(newVariable("orders_drink").forField("drink")) .pipeline(match(ctx -> new Document("$expr", new Document("$in", List.of("$$orders_drink", "$beverages"))))) .as("matches")- Returns:
- new instance of
LookupOperation.LookupOperationBuilder. - Since:
- 4.1
-
count
Creates a newCountOperation.CountOperationBuilder.- Returns:
- never null.
- Since:
- 1.10
-
redact
Creates a newRedactOperationthat can restrict the content of a document based on information stored within the document itself.Aggregation.redact(ConditionalOperators.when(Criteria.where("level").is(5)) // .then(RedactOperation.PRUNE) // .otherwise(RedactOperation.DESCEND));- Parameters:
condition- AnyAggregationExpressionthat resolves to $$DESCEND, $$PRUNE, or $$KEEP. Must not be null.- Returns:
- new instance of
RedactOperation. Never null. - Since:
- 3.0
-
fields
Creates a newFieldsinstance for the given field names.- Parameters:
fields- must not be null.- Returns:
- new instance of
Fields. - See Also:
-
bind
Creates a newFieldsinstance from the given field name and target reference.- Parameters:
name- must not be null or empty.target- must not be null or empty.- Returns:
- new instance of
Fields.
-
newAggregationOptions
Returns a newAggregationOptions.Builder.- Returns:
- new instance of
AggregationOptions.Builder. - Since:
- 1.6
-
toPipeline
- Returns:
- the aggregation pipeline representing this aggregation.
- Since:
- 2.1
-
getPipeline
- Returns:
- the
AggregationPipeline. - Since:
- 3.0.2
-
toDocument
public org.bson.Document toDocument(String inputCollectionName, AggregationOperationContext rootContext) Converts thisAggregationspecification to aDocument.
MongoDB requires as of 3.6 cursor-based aggregation. UsetoPipeline(AggregationOperationContext)to render an aggregation pipeline.- Parameters:
inputCollectionName- the name of the input collection.- Returns:
- the
Documentrepresenting this aggregation.
-
toString
-