Mongo聚合两个字符串的总和

huangapple go评论63阅读模式
英文:

Mongo aggregate sum of two Strings

问题

amountField是一个字符串,我该如何在这个实现中对它进行求和?

英文:

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(
                Aggregation.match(
                        Criteria.where("rawRequest.agentId").is(agentId)
                                .and("status").is(status)
                                .and("dateCreated").gte(date)),
                Aggregation.group("rawRequest.agentId")
                        .sum(amountField).as("amount"),
                Aggregation.project().andExclude("_id"));
        HashMap results = mongoTemplate
                .aggregate(aggregationAmount, "underwritingRequest", HashMap.class)
                .getUniqueMappedResult();
        return results == null ? 0 : (Integer)results.get("totalAmount");
    }

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

答案1

得分: 0

你正在获取String amountField。你可以简单地通过在聚合中使用int x=Integer.parseInt(s)将这个字符串转换为整数,并在聚合内部使用它。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月3日 20:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63229732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定