|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.aggregation; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.bson.Document; |
| 22 | +import org.springframework.lang.Nullable; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * The <a href="https://docs.mongodb.com/master/reference/operator/aggregation/unionWith/">$unionWith</a> aggregation |
| 27 | + * stage (available since MongoDB 4.4) performs a union of two collections by combining pipeline results, potentially |
| 28 | + * containing duplicates, into a single result set that is handed over to the next stage. <br /> |
| 29 | + * In order to remove duplicates it is possible to append a {@link GroupOperation} right after |
| 30 | + * {@link UnionWithOperation}. |
| 31 | + * <p /> |
| 32 | + * If the {@link UnionWithOperation} uses a |
| 33 | + * <a href="https://docs.mongodb.com/master/reference/operator/aggregation/unionWith/#unionwith-pipeline">pipeline</a> |
| 34 | + * to process documents, field names within the pipeline will be treated as is. In order to map domain type property |
| 35 | + * names to actual field names (considering potential {@link org.springframework.data.mongodb.core.mapping.Field} |
| 36 | + * annotations) make sure the enclosing aggregation is a {@link TypedAggregation} and provide the target type for the |
| 37 | + * {@code $unionWith} stage via {@link #mapFieldsTo(Class)}. |
| 38 | + * |
| 39 | + * @author Christoph Strobl |
| 40 | + * @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/unionWith/">Aggregation Pipeline Stage: |
| 41 | + * $unionWith</a> |
| 42 | + * @since 3.1 |
| 43 | + */ |
| 44 | +public class UnionWithOperation implements AggregationOperation { |
| 45 | + |
| 46 | + private final String collection; |
| 47 | + |
| 48 | + @Nullable // |
| 49 | + private final AggregationPipeline pipeline; |
| 50 | + |
| 51 | + @Nullable // |
| 52 | + private final Class<?> domainType; |
| 53 | + |
| 54 | + public UnionWithOperation(String collection, @Nullable AggregationPipeline pipeline, @Nullable Class<?> domainType) { |
| 55 | + |
| 56 | + this.collection = collection; |
| 57 | + this.pipeline = pipeline; |
| 58 | + this.domainType = domainType; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Set the name of the collection from which pipeline results should be included in the result set.<br /> |
| 63 | + * The collection name is used to set the {@code coll} parameter of {@code $unionWith}. |
| 64 | + * |
| 65 | + * @param collection the MongoDB collection name. Must not be {@literal null}. |
| 66 | + * @return new instance of {@link UnionWithOperation}. |
| 67 | + * @throws IllegalArgumentException if the required argument is {@literal null}. |
| 68 | + */ |
| 69 | + public static UnionWithOperation unionWith(String collection) { |
| 70 | + |
| 71 | + Assert.notNull(collection, "Collection must not be null!"); |
| 72 | + return new UnionWithOperation(collection, null, null); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set the {@link AggregationPipeline} to apply to the specified collection. The pipeline corresponds to the optional |
| 77 | + * {@code pipeline} field of the {@code $unionWith} aggregation stage and is used to compute the documents going into |
| 78 | + * the result set. |
| 79 | + * |
| 80 | + * @param pipeline the {@link AggregationPipeline} that computes the documents. Must not be {@literal null}. |
| 81 | + * @return new instance of {@link UnionWithOperation}. |
| 82 | + * @throws IllegalArgumentException if the required argument is {@literal null}. |
| 83 | + */ |
| 84 | + public UnionWithOperation pipeline(AggregationPipeline pipeline) { |
| 85 | + return new UnionWithOperation(collection, pipeline, domainType); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the aggregation pipeline stages to apply to the specified collection. The pipeline corresponds to the optional |
| 90 | + * {@code pipeline} field of the {@code $unionWith} aggregation stage and is used to compute the documents going into |
| 91 | + * the result set. |
| 92 | + * |
| 93 | + * @param aggregationStages the aggregation pipeline stages that compute the documents. Must not be {@literal null}. |
| 94 | + * @return new instance of {@link UnionWithOperation}. |
| 95 | + * @throws IllegalArgumentException if the required argument is {@literal null}. |
| 96 | + */ |
| 97 | + public UnionWithOperation pipeline(List<AggregationOperation> aggregationStages) { |
| 98 | + return new UnionWithOperation(collection, new AggregationPipeline(aggregationStages), domainType); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set the aggregation pipeline stages to apply to the specified collection. The pipeline corresponds to the optional |
| 103 | + * {@code pipeline} field of the {@code $unionWith} aggregation stage and is used to compute the documents going into |
| 104 | + * the result set. |
| 105 | + * |
| 106 | + * @param aggregationStages the aggregation pipeline stages that compute the documents. Must not be {@literal null}. |
| 107 | + * @return new instance of {@link UnionWithOperation}. |
| 108 | + * @throws IllegalArgumentException if the required argument is {@literal null}. |
| 109 | + */ |
| 110 | + public UnionWithOperation pipeline(AggregationOperation... aggregationStages) { |
| 111 | + return new UnionWithOperation(collection, new AggregationPipeline(Arrays.asList(aggregationStages)), domainType); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Set domain type used for field name mapping of property references used by the {@link AggregationPipeline}. |
| 116 | + * Remember to also use a {@link TypedAggregation} in the outer pipeline.<br /> |
| 117 | + * If not set, field names used within {@link AggregationOperation pipeline operations} are taken as is. |
| 118 | + * |
| 119 | + * @param domainType the domain type to map field names used in pipeline operations to. Must not be {@literal null}. |
| 120 | + * @return new instance of {@link UnionWithOperation}. |
| 121 | + * @throws IllegalArgumentException if the required argument is {@literal null}. |
| 122 | + */ |
| 123 | + public UnionWithOperation mapFieldsTo(Class<?> domainType) { |
| 124 | + |
| 125 | + Assert.notNull(domainType, "DomainType must not be null!"); |
| 126 | + return new UnionWithOperation(collection, pipeline, domainType); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + * (non-Javadoc) |
| 131 | + * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public Document toDocument(AggregationOperationContext context) { |
| 135 | + |
| 136 | + Document $unionWith = new Document("coll", collection); |
| 137 | + if (pipeline == null || pipeline.isEmpty()) { |
| 138 | + return new Document(getOperator(), $unionWith); |
| 139 | + } |
| 140 | + |
| 141 | + $unionWith.append("pipeline", pipeline.toDocuments(computeContext(context))); |
| 142 | + return new Document(getOperator(), $unionWith); |
| 143 | + } |
| 144 | + |
| 145 | + private AggregationOperationContext computeContext(AggregationOperationContext source) { |
| 146 | + |
| 147 | + if (domainType == null) { |
| 148 | + return Aggregation.DEFAULT_CONTEXT; |
| 149 | + } |
| 150 | + |
| 151 | + if (source instanceof TypeBasedAggregationOperationContext) { |
| 152 | + return ((TypeBasedAggregationOperationContext) source).continueOnMissingFieldReference(domainType); |
| 153 | + } |
| 154 | + |
| 155 | + if (source instanceof ExposedFieldsAggregationOperationContext) { |
| 156 | + return computeContext(((ExposedFieldsAggregationOperationContext) source).getRootContext()); |
| 157 | + } |
| 158 | + |
| 159 | + return source; |
| 160 | + } |
| 161 | + |
| 162 | + /* |
| 163 | + * (non-Javadoc) |
| 164 | + * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#getOperator() |
| 165 | + */ |
| 166 | + @Override |
| 167 | + public String getOperator() { |
| 168 | + return "$unionWith"; |
| 169 | + } |
| 170 | +} |
0 commit comments