简体   繁体   English

来自数组的汇总值

[英]Aggregated values from array

I'm trying to calculate sum of balances divided to currencies. 我正在尝试计算除以货币的余额总和。 Below example document and my query. 下面的示例文件和我的查询。 Query returns list of currencies with sumBalance equal 0 for all right now. 查询立即返回sumBalance等于0的货币列表。 Any advices how to fix it? 有什么建议如何解决吗?

{
    _id:5ab94cdb6b106375b6843358   
    credit:
    Array 
0:
    Object
    type:"mastercard" 
    currency:"BRL"
    balance:"4627.56"
1:
    Object
    type:"diners-club-enroute"
    currency:"USD"
    balance:"5222.47"
}

db.mydb.find().forEach( function (x) {
x.credit.balance = parseFloat(x.credit.balance);
db.mydb.save(x);
});
db.mydb.aggregate([
{$unwind: "$credit"},
{$group: {_id: "$credit.currency", sumBalance: {$sum: "$credit.balance"}}}
]).forEach(printjson)

Your problem is that your balance is a String and not a number and $aggregation can't change the data type. 您的问题是您的余额是字符串而不是数字,并且$ aggregation不能更改数据类型。 I suggest you to update your document to have the balance as Float 我建议您更新文档以使余额为Float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM