Open
Description
I need to use custom AggregationOperation for $group
operation in my aggregation query. So I've created class like this:
public class CustomGroupingOperation implements AggregationOperation {
@Override
public Document toDocument(AggregationOperationContext context) {
return null;
}
@Override
public List<Document> toPipelineStages(AggregationOperationContext context) {
return List.of(new Document("$group",
new Document("_id", new Document("name", new Document("$ifNull", new Object[] {"$name", null})))
.append("records", new Document("$push", "$$ROOT"))));
}
@Override
public String getOperator() {
return AggregationOperation.super.getOperator();
}
}
According to documentation, toDocument
method is deprecated in favor of toPipelineStages
method. However, when I use this class with mongoTemplate like this:
AggregationOperation grouping = new CustomGroupingOperation();
TypedAggregation<MyPojoClass> aggregation = newAggregation(
Object.class,
match
grouping
);
return mongoTemplate.aggregate(aggregation, MyPojoClass.class).getMappedResults();
During debugging I see that application enters toDocument
method instead of toPipelineStages
.
Should I change something in my approach, or is this a bug?
UPD Here are my drivers from spring-boot-starter-data-mongodb-2.7.2.pom
:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.4.2</version>
<scope>compile</scope>
</dependency>