Module spring.data.mongodb
Class UnionWithOperation
java.lang.Object
org.springframework.data.mongodb.core.aggregation.UnionWithOperation
- All Implemented Interfaces:
AggregationOperation
The $unionWith aggregation
stage (available since MongoDB 4.4) performs a union of two collections by combining pipeline results, potentially
containing duplicates, into a single result set that is handed over to the next stage.
In order to remove duplicates it is possible to append a
If the
In order to remove duplicates it is possible to append a
GroupOperation right after
UnionWithOperation.
If the
UnionWithOperation uses a
pipeline
to process documents, field names within the pipeline will be treated as is. In order to map domain type property
names to actual field names (considering potential Field
annotations) make sure the enclosing aggregation is a TypedAggregation and provide the target type for the
$unionWith stage via mapFieldsTo(Class).- Since:
- 3.1
- Author:
- Christoph Strobl
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionUnionWithOperation(String collection, AggregationPipeline pipeline, Class<?> domainType) -
Method Summary
Modifier and TypeMethodDescriptionReturn the MongoDB operator that is used for thisAggregationOperation.mapFieldsTo(Class<?> domainType) Set domain type used for field name mapping of property references used by theAggregationPipeline.pipeline(List<AggregationOperation> aggregationStages) Set the aggregation pipeline stages to apply to the specified collection.pipeline(AggregationOperation... aggregationStages) Set the aggregation pipeline stages to apply to the specified collection.pipeline(AggregationPipeline pipeline) Set theAggregationPipelineto apply to the specified collection.org.bson.DocumenttoDocument(AggregationOperationContext context) static UnionWithOperationSet the name of the collection from which pipeline results should be included in the result set.
The collection name is used to set thecollparameter of$unionWith.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.mongodb.core.aggregation.AggregationOperation
toPipelineStages
-
Constructor Details
-
UnionWithOperation
public UnionWithOperation(String collection, @Nullable AggregationPipeline pipeline, @Nullable Class<?> domainType)
-
-
Method Details
-
unionWith
Set the name of the collection from which pipeline results should be included in the result set.
The collection name is used to set thecollparameter of$unionWith.- Parameters:
collection- the MongoDB collection name. Must not be null.- Returns:
- new instance of
UnionWithOperation. - Throws:
IllegalArgumentException- if the required argument is null.
-
pipeline
Set theAggregationPipelineto apply to the specified collection. The pipeline corresponds to the optionalpipelinefield of the$unionWithaggregation stage and is used to compute the documents going into the result set.- Parameters:
pipeline- theAggregationPipelinethat computes the documents. Must not be null.- Returns:
- new instance of
UnionWithOperation. - Throws:
IllegalArgumentException- if the required argument is null.
-
pipeline
Set the aggregation pipeline stages to apply to the specified collection. The pipeline corresponds to the optionalpipelinefield of the$unionWithaggregation stage and is used to compute the documents going into the result set.- Parameters:
aggregationStages- the aggregation pipeline stages that compute the documents. Must not be null.- Returns:
- new instance of
UnionWithOperation. - Throws:
IllegalArgumentException- if the required argument is null.
-
pipeline
Set the aggregation pipeline stages to apply to the specified collection. The pipeline corresponds to the optionalpipelinefield of the$unionWithaggregation stage and is used to compute the documents going into the result set.- Parameters:
aggregationStages- the aggregation pipeline stages that compute the documents. Must not be null.- Returns:
- new instance of
UnionWithOperation. - Throws:
IllegalArgumentException- if the required argument is null.
-
mapFieldsTo
Set domain type used for field name mapping of property references used by theAggregationPipeline. Remember to also use aTypedAggregationin the outer pipeline.
If not set, field names used withinpipeline operationsare taken as is.- Parameters:
domainType- the domain type to map field names used in pipeline operations to. Must not be null.- Returns:
- new instance of
UnionWithOperation. - Throws:
IllegalArgumentException- if the required argument is null.
-
toDocument
Description copied from interface:AggregationOperation- Specified by:
toDocumentin interfaceAggregationOperation- Parameters:
context- theAggregationOperationContextto operate within. Must not be null.- Returns:
- the Document
-
getOperator
Description copied from interface:AggregationOperationReturn the MongoDB operator that is used for thisAggregationOperation. Aggregation operations should implement this method to avoid document rendering.- Specified by:
getOperatorin interfaceAggregationOperation- Returns:
- the operator used for this
AggregationOperation.
-