Mongo聚合两个字符串的总和

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

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

  1. private Integer findRequestAmount(String agentId, String status, Date date, String amountField) {
  2. Aggregation aggregationAmount = Aggregation.newAggregation(
  3. Aggregation.match(
  4. Criteria.where("rawRequest.agentId").is(agentId)
  5. .and("status").is(status)
  6. .and("dateCreated").gte(date)),
  7. Aggregation.group("rawRequest.agentId")
  8. .sum(amountField).as("amount"),
  9. Aggregation.project().andExclude("_id"));
  10. HashMap results = mongoTemplate
  11. .aggregate(aggregationAmount, "underwritingRequest", HashMap.class)
  12. .getUniqueMappedResult();
  13. return results == null ? 0 : (Integer)results.get("totalAmount");
  14. }

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:

确定