我试图在这种数据集上执行MapReduce:
{
"_id": "599861ce7ce78cd973746906",
"name": "Macias Rosario",
"col": [
{
"date": "15/03/2016",
"name": "MAGNEATO",
"amount": 313.86
},
{
"date": "08/08/2016",
"name": "FORTEAN",
"amount": 151.06
},
{
"date": "05/11/2014",
"name": "ECRATIC",
"amount": 291.68
}
]
}
目标是总结名称 Macias Rosario 的所有金额。目前,我使用我的代码按照这种方式将所有子元素this.col.name
分组:
mapper = Code("""
function() {
for (var index = 0; index < this.col.length; ++index) {
var col = this.col[index];
emit(col.name, col.amount );
}
}
""")
reducer = Code("""
function(key, values) {
var total = 0;
for( var i = 0; i < values.length; ++i){
total += values[i];
}
return value.price;
}
""")
result = collection.map_reduce(mapper, reducer, "myresult")
有没有人知道如何引用,或按this.name
而不是this.col.name
进行分组,因为我不再知道并且我在驾驶坚果?
PS不建议我使用aggregate
,这样做,也想尝试这种方式:)
亲切的问候,
答案 0 :(得分:0)
我希望以下代码有用(也适用于pymongo)
这是我的地图功能:
SendMessage
以下是我的缩减功能:
var mapFunction = function() {
for (var idx = 0; idx < this.col.length; idx++) {
var key = this.name;
var value = { amount : this.col[idx].amount };
emit(key, value);
}
};
使用您的样本数据:
var reduceFunction = function(key, amountVl) {
reduceVal = { amount : 0 };
for (var idx = 0; idx < amountVl.length; idx++) {
reduceVal.amount += amountVl[idx].amount;
}
return reduceVal;
}