|
29 | 29 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
30 | 30 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
31 | 31 | import org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation.InheritsFieldsAggregationOperation;
|
| 32 | +import org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.ReplaceRootDocumentOperationBuilder; |
| 33 | +import org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.ReplaceRootOperationBuilder; |
32 | 34 | import org.springframework.data.mongodb.core.aggregation.Fields.*;
|
33 | 35 | import org.springframework.data.mongodb.core.query.Criteria;
|
34 | 36 | import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
@@ -216,6 +218,40 @@ public static UnwindOperation unwind(String field) {
|
216 | 218 | return new UnwindOperation(field(field));
|
217 | 219 | }
|
218 | 220 |
|
| 221 | + /** |
| 222 | + * Factory method to create a new {@link ReplaceRootOperation} for the field with the given name. |
| 223 | + * |
| 224 | + * @param fieldName must not be {@literal null} or empty. |
| 225 | + * @return |
| 226 | + * @since 1.10 |
| 227 | + */ |
| 228 | + public static ReplaceRootOperation replaceRoot(String fieldName) { |
| 229 | + return ReplaceRootOperation.builder().withValueOf(fieldName); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Factory method to create a new {@link ReplaceRootOperation} for the field with the given |
| 234 | + * {@link AggregationExpression}. |
| 235 | + * |
| 236 | + * @param aggregationExpression must not be {@literal null}. |
| 237 | + * @return |
| 238 | + * @since 1.10 |
| 239 | + */ |
| 240 | + public static ReplaceRootOperation replaceRoot(AggregationExpression aggregationExpression) { |
| 241 | + return ReplaceRootOperation.builder().withValueOf(aggregationExpression); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Factory method to create a new {@link ReplaceRootDocumentOperationBuilder} to configure a |
| 246 | + * {@link ReplaceRootOperation}. |
| 247 | + * |
| 248 | + * @return the {@literal ReplaceRootDocumentOperationBuilder}. |
| 249 | + * @since 1.10 |
| 250 | + */ |
| 251 | + public static ReplaceRootOperationBuilder replaceRoot() { |
| 252 | + return ReplaceRootOperation.builder(); |
| 253 | + } |
| 254 | + |
219 | 255 | /**
|
220 | 256 | * Factory method to create a new {@link UnwindOperation} for the field with the given name and
|
221 | 257 | * {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in MongoDB version 3.2+.
|
@@ -468,11 +504,13 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte
|
468 | 504 | if (operation instanceof FieldsExposingAggregationOperation) {
|
469 | 505 |
|
470 | 506 | FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation;
|
| 507 | + ExposedFields fields = exposedFieldsOperation.getFields(); |
471 | 508 |
|
472 | 509 | if (operation instanceof InheritsFieldsAggregationOperation) {
|
473 |
| - context = new InheritingExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), context); |
| 510 | + context = new InheritingExposedFieldsAggregationOperationContext(fields, context); |
474 | 511 | } else {
|
475 |
| - context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), context); |
| 512 | + context = fields.exposesNoFields() ? DEFAULT_CONTEXT |
| 513 | + : new ExposedFieldsAggregationOperationContext(fields, context); |
476 | 514 | }
|
477 | 515 | }
|
478 | 516 | }
|
|
0 commit comments