Tyler MacMillan
Tyler MacMillan

Reputation: 592

Utilizing ConvertOperators in an aggregation pipeline

I am using the below code. It works exactly as I intend it to.

List<Bson> aggregatePipeline = Arrays.asList(
    Aggregates.match(Filters.and(
            Filters.gte(ACTIVITY_DATE, job.getActivityStartDate()),
            Filters.lte(ACTIVITY_DATE, job.getActivityEndDate()))),
    Aggregates.group("$" + COLLECTOR_STAGE_ID, List.of(
            new BsonField("mongoRateState",
                    new Document("$addToSet",
                            new Document("$ne", Arrays.asList("$" + COST_UNROUNDED, null)))),
            Accumulators.sum(QUANTITY, new Document("$toDecimal", "$" + QUANTITY)),
            Accumulators.sum("mongoCost", new Document("$toDecimal", "$" + COST_UNROUNDED))
    )));

mongoTemplate.getCollection(usageCollection).aggregate(aggregatePipeline).allowDiskUse(true).first();

I am trying to utilize ConvertOperators.ToDecimal.toDecimal() to simplify the code, and replace

new Document("$toDecimal", "$" + QUANTITY))

with a statically imported call like so:

toDecimal("$" + QUANTITY)

However, this results in the error:

Can't find a codec for class:
org.springframework.data.mongodb.core.aggregation.ConvertOperators$ToDecimal

So it is obvious I am using this method in an unintended way. Am I stuck passing in a new Document, or is there a better way to accomplish what I am attempting?

spring-data-mongodb version: 3.4.18

Upvotes: 0

Views: 24

Answers (0)

Related Questions