Use $mergeObjects inside ReplaceRoot pipeline stage in Spring MongoDB
You have couple of issues here. Use 2.1.0 Release Spring Mongodb jar.
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ObjectOperators.valueOf("arrayForeignObject").mergeWith(Aggregation.ROOT));AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);
For lower versions of spring mongodb
AggregationOperation replaceRoot = ReplaceRootOperation.builder().withDocument("$mergeObjects", Arrays.asList("$arrayForeignObject", Aggregation.ROOT));AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);