我尝试对所有我的金额值求和,这些值作为NumberDecimal数据类型存储在mongodb中。
这是我编写的map reduce代码。
mapper = """
function() {
const key = this.time.toLocaleDateString('zh-CN');
emit(key, {
ids: [this.distinct_id],
unique: 1,
count: 1,
date: key,
avg: 0,
amount: this.amount, // the amount I want to sum
});
}
"""
reducer = """
function (key, values) {
const idset = new Set();
var count = 0;
var amount = 0;
values.forEach(value => {
value.ids.forEach(id => { idset.add(id); });
count += value.count;
amount += value.amount; // here I added all up.
});
return {
ids: Array.from(idset),
unique: idset.size,
count: count,
date: key,
amount: amount,
}
}
"""
这是我得到的结果。似乎所有的值都由字符串连接起来!
{data: [{
amount: "0000000NumberDecimal("1486.72")NumberDecimal("1572.06")NumberDecimal("720.04")NumberDecimal("276.23")....."
]}