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
ConstructorDescriptionUnionWithOperation
(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 theAggregationPipeline
to apply to the specified collection.org.bson.Document
toDocument
(AggregationOperationContext context) static UnionWithOperation
Set the name of the collection from which pipeline results should be included in the result set.
The collection name is used to set thecoll
parameter of$unionWith
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 thecoll
parameter 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 theAggregationPipeline
to apply to the specified collection. The pipeline corresponds to the optionalpipeline
field of the$unionWith
aggregation stage and is used to compute the documents going into the result set.- Parameters:
pipeline
- theAggregationPipeline
that 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 optionalpipeline
field of the$unionWith
aggregation 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 optionalpipeline
field of the$unionWith
aggregation 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 aTypedAggregation
in the outer pipeline.
If not set, field names used withinpipeline operations
are 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:
toDocument
in interfaceAggregationOperation
- Parameters:
context
- theAggregationOperationContext
to operate within. Must not be null.- Returns:
- the Document
-
getOperator
Description copied from interface:AggregationOperation
Return the MongoDB operator that is used for thisAggregationOperation
. Aggregation operations should implement this method to avoid document rendering.- Specified by:
getOperator
in interfaceAggregationOperation
- Returns:
- the operator used for this
AggregationOperation
.
-