Skip to content

Commit ae4cfaa

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1550 - Add $replaceRoot aggregation stage.
We now support the $replaceRoot stage in aggregation pipelines. $replaceRoot can reference either a field, an aggregation expression or it can be used to compose a replacement document. newAggregation( replaceRoot().withDocument() .andValue("value").as("field") .and(MULTIPLY.of(field("total"), field("discounted"))) ); newAggregation( replaceRoot("item"))); Original Pull Request: spring-projects#422
1 parent 1dea009 commit ae4cfaa

File tree

6 files changed

+771
-3
lines changed

6 files changed

+771
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

+40-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
3030
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
3131
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;
3234
import org.springframework.data.mongodb.core.aggregation.Fields.*;
3335
import org.springframework.data.mongodb.core.query.Criteria;
3436
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
@@ -216,6 +218,40 @@ public static UnwindOperation unwind(String field) {
216218
return new UnwindOperation(field(field));
217219
}
218220

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+
219255
/**
220256
* Factory method to create a new {@link UnwindOperation} for the field with the given name and
221257
* {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in MongoDB version 3.2+.
@@ -468,11 +504,13 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte
468504
if (operation instanceof FieldsExposingAggregationOperation) {
469505

470506
FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation;
507+
ExposedFields fields = exposedFieldsOperation.getFields();
471508

472509
if (operation instanceof InheritsFieldsAggregationOperation) {
473-
context = new InheritingExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), context);
510+
context = new InheritingExposedFieldsAggregationOperationContext(fields, context);
474511
} else {
475-
context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), context);
512+
context = fields.exposesNoFields() ? DEFAULT_CONTEXT
513+
: new ExposedFieldsAggregationOperationContext(fields, context);
476514
}
477515
}
478516
}

0 commit comments

Comments
 (0)