英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论