I want to use mapreduce to perform the group aggregation.
Here is my map function :
function() {
emit(this.TransactionType, { Count: 1 });
}
Here is two reduce functions :
function(key, values) {
var result = {Count: 0};
values.forEach(function(value) {
result.Count += 1;
});
return result;
}
function(key, values) {
var result = {Count: 0};
values.forEach(function(value) {
result.Count += value.Count;
});
return result;
}
and here is the two results :
"_id" : "A", "value" : { "Count" : 13.0 }
"_id" : "B", "value" : { "Count" : 2.0 }
"_id" : "C", "value" : { "Count" : 1.0 }
"_id" : "D", "value" : { "Count" : 209.0 }
"_id" : "E", "value" : { "Count" : 66.0 }
"_id" : "F", "value" : { "Count" : 11.0 }
"_id" : "G", "value" : { "Count" : 17.0 }
"_id" : "H", "value" : { "Count" : 17.0 }
"_id" : "A", "value" : { "Count" : 128.0 }
"_id" : "B", "value" : { "Count" : 115.0 }
"_id" : "C", "value" : { "Count" : 1.0 }
"_id" : "D", "value" : { "Count" : 3645.0 }
"_id" : "E", "value" : { "Count" : 1405.0 }
"_id" : "F", "value" : { "Count" : 256.0 }
"_id" : "G", "value" : { "Count" : 380.0 }
"_id" : "H", "value" : { "Count" : 398.0 }
So why the two results are different?
Thank you very much
It’s helpful to think of the “reduce” function in terms of the “fold” higher-order function. That is to say, your “reduce” function will be applied to a list of values and an accumulated object (the “result” variable in your examples), which is initially specified but will eventually be replaced by the output of successive calls to your function. More over, the list of values to which your function will be applied can be broken up into any number of sub-lists, in any order!
For example, consider how your function would behave using the JavaScript Array “reduce” function, which is an example of the “fold” higher-order function. Your first example will behave improperly because it doesn’t use the “Count” property of each element. Successive attempts to use it with Array#reduce will fail similarly:
However, your second example properly adds the “Count” property and can be applied successively to its own output: