0

I want to sum the values of a column price.value where the column validatedAt is between startDate and endDate.
So I want a BigInteger as a result (the sum value).

This is what I tried:

        final List<AggregationOperation> aggregationOperations = new ArrayList<>();
        aggregationOperations.add(Aggregation.match(where("validatedAt").gte(startDate).lt(endDate)));
        aggregationOperations.add(Aggregation.group().sum("price.value").as("total"));

        final Aggregation turnoverAggregation = Aggregation.newAggregation(OrderEntity.class, aggregationOperations);

        return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, BigInteger.class).getUniqueMappedResult();

This doesn't work. I have this error:

{"exception":"com.application.CommonClient$Builder$6","path":"/api/stats/dashboard","message":"Failed to instantiate java.math.BigInteger using constructor NO_CONSTRUCTOR with arguments ","error":"Internal Server Error","timestamp":1493209463953,"status":500}

Any help?

3
  • Have you tried using a Long? I'm not sure why you expect a BigInteger Commented Apr 26, 2017 at 12:27
  • The error makes sense if you look at the BigInteger JavaDoc. The isn't a constructor with no arguments Commented Apr 26, 2017 at 12:28
  • Long does not work. What should I put then? Commented Apr 26, 2017 at 12:35

2 Answers 2

1

You don`t need to add a new pojo for just for this. It is helpful when you more fields to map and you want spring to map them automatically.

The correct way to fix the problem is to use BasicDBObject because MongoDB stores all values as key value pairs.

return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, BasicDBObject.class).getUniqueMappedResult().getInt("total");

Sidenote: You should use Double/BigDecimal for monetary values.

Sign up to request clarification or add additional context in comments.

Comments

0

I resolved this problem by creating a class that has a BigInteger attribute:

private class Total {
    private int total;
}

 return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, Total.class).getUniqueMappedResult();

1 Comment

That is a regular int, not BigInteger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.