时间:2021-07-01 10:21:17 帮助过:11人阅读
执行过程:
map (映射) 函数来处理每个文档:cust_id, 并处理 itemsitems,分别对每个items成员 qty和price相乘再求总和var mapFunction2 = function() {
var key = this.cust_id;
var value = 0;
for (var idx = 0; idx < this.items.length; idx++) {
value += this.items[idx].qty * this.items[idx].price;
}
emit(key, value);
};
valuesPrices 是数组,由 keyCustId 分组, 收集 value 而来reduces 函数 对 valuesPrices 数组 求和.var reduceFunction2 = function(keyCustId, valuesPrices) {
return Array.sum(valuesPrices);
};
db.orders.mapReduce(
mapFunction2,
reduceFunction2,
{ out: "map_reduce_example" }
)mongodb MapReduce
标签:key 定义 out limit var value item nbsp oct