Mongo aggregate sum of two Strings

Tags: , , ,

I am trying to sum one field in Mongo. If it is an Integer there is no issue, but the problem is that it is String and I get 0 for totalAmount

private Integer findRequestAmount(String agentId, String status, Date date, String amountField) {
        Aggregation aggregationAmount = Aggregation.newAggregation(
        HashMap results = mongoTemplate
                .aggregate(aggregationAmount, "underwritingRequest", HashMap.class)
        return results == null ? 0 : (Integer)results.get("totalAmount");

amountField is a String, how can I sum it in this implementation


You are getting String amountField. Simply you can convert this String to Integer by using int x=Integer.parseInt(s) over the aggregation and use it inside the aggregation.

Source: stackoverflow