【发布时间】:2021-05-15 20:12:55
【问题描述】:
我正在尝试将字符串字段转换为双精度字段,同时聚合包含空格的字段名称。
但输入字段 $+sumField 解析为带有空格的 $Total Incl Vat,我认为这是不正确的,它不会返回实际总和。
谁有解决办法?
示例数据
{_id: 1, "Total Incl Vat": "1550.96", customer: 1},
{_id: 2, "Total Incl Vat": "2000", customer: 1},
{_id: 3, "Total Incl Vat": "1000", customer: 1}
聚合
const sumField = "Total Incl Vat";
const $group = {
_id: null,
total: {
$sum: {
$convert: {
input: "$" + sumField,
to: 'double',
onNull: 0,
onError: "Error"
}
}
}
}
const result = mycollection.aggregate([
{ $match: { customer: 1 }},
{ $group }
]);
聚合结果为 0,这是不正确的结果。
【问题讨论】:
标签: string mongoose double aggregate