Database Reference
In-Depth Information
db.mapreduce.mapReduce(map,reduce,{ out: "mrresult" });
Now Let's check those results quickly:
> db.mrresult.findOne();
{
"_id" : "black",
"value" : {
"num" : 18381,
"count" : 27028,
}
}
No, there is not an average value. This means we have more work to do, but how do we make the average given
that we have to return a document that matches the input of the emit? We need to a third function! MapRreduce
provides one, called the finalize function. This allows you to do any final cleanup before returning your MapReduce
results. Let's write a function that will take the result from reduce and calculate the average for us:
var finalize = function (key, value) {
value.avg = value.num/value.count;
return value;
};
Yes, it's that simple. So now with our map , reduce , and finalize functions ready, we simply add them to our call.
The finalize option is set in the last document; along with the out , this gives us the following command:
db.mapreduce.mapReduce(map,reduce,{ out: "mrresult", finalize : finalize });
And from here let's query one of our example documents:
> db.mrresult.findOne();
{
"_id" : "black",
"value" : {
"num" : 45318,
"count" : 91,
"avg" : 498
}
}
Now that's better! We have our number, our count, and our average!
Debugging MapReduce
Debugging Map/Reduce is quite a time-consuming task, but there are a few little tricks to make your life easier. First
let's look at debugging a map . You can debug a map by overloading the emit with a function, as shown here:
var emit = function(key, value) {
print("emit results - key: " + key + " value: " + tojson(value));
}
 
Search WWH ::




Custom Search