Start a new topic

finalize no longer called during MapReduce

It seems that some time over the course of the last week or so, the finalize function of my mapreduce is no longer getting called and I can't figure out why. I have created a contrived example that reproduces the problem:

function onRequest(request, response, modules){

function map() {

emit(this.username[0], 1);


function reduce(key, values) {

var count = 0;

for (var i = 0; i
count += values[i];

return count / 1000000; // force some decimal places


function finalize(key, reducedValue) {

return reducedValue.toFixed(decimalPlaces);






query: {},

scope: {

decimalPlaces: 2


finalize: finalize


function(err, result) {

response.body = err || result;





Basically it iterates over all users and emits the first letter of their username and then this is counted in the reduce step. The count is divided by some large number to artificially add some decimal places and then the finalize function should format the results to 2 decimal places by calling toFixed. The number of decimal places is passed in via the scope object. Just dump this script into a custom endpoint and run it via API console and you can see the values have more than 2 decimal places.

We had to remove the finalize option because of a security issue we uncovered. Longer-term, we are working on migrating to Mongo's newer aggregation framework, which will give us more flexibility in our aggregation APIs. In the meantime, a workaround would be to retrieve the results without finalize, and to then implement your finalize logic using one of the async module's settings.
Ok that would explain it. I worked around the issue by ensuring both my map and reduce function return the correctly formatted string, but it requires an extra type conversion in the reduce function which is suboptimal, but not a big deal.

Just out of curiosity, where was this change communicated? I didn't see anything in the timeline. Knowing about it in advance would have saved me some debugging time :)
Login or Signup to post a comment