spring-projects / spring-projects/spring-data-mongodb

AggregationOperation::toDocument method is deprecated, but MongoTemplate still uses it

Open
#4,306 1 comment 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Feb 27, 2023.

  • #4309 by @christophstrobl — open
type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.