When using map/reduce on MongoDb 2.0.1, it seems that the finalize method is not always called with the same arguments.
I am doing a regular counting + average at the end.
var m = function() {
emit(this.testName, {
worked: Number(this.testStatus == "WORKED"),
failed: Number(this.testStatus == "FAILED"),
done: Number(this.testStatus == "DONE"),
count: 1
});
}
var r = function(key, values) {
var result = {
worked: 0,
failed: 0,
done: 0,
count: 0
}
values.forEach(function(value) {
result.worked += value.worked;
result.failed += value.failed;
result.done += value.done;
result.count += value.count;
});
return result;
}
var f = function(key, value) {
data = value || key;
data.workedMean = data.worked / data.count;
return data;
}
var cmd = {
mapreduce: "tests",
map: m,
reduce: r,
finalize: f,
out: {
reduce: "fromMongo"
},
jsMode: true
}
When the fromMongo collection is empty, f() is called with only one argument, the value. When fromMongo already has values (notice that I use reduce as my out map/reduce parameter), the f() method get two arguments: key and value.
Is that a known behavior?
I managed two make it work using data = value || key;, but I don’t think this is the solution.
this problem is due to a difference in finalize call between the jsMode and non-jsMode (which happens during the post processing of “reduce” mode).
The workaround is to not use jsMode:true, in which case it should always be called with (key, value).
Created a server issue:
https://jira.mongodb.org/browse/SERVER-4535