I've written a MapReduce in MongoDB and would like to use a global variable as a cache to write to/read from. I know it is not possible to have global variables across map function instances - I just want a global variable within each function instance. This type of functionality exists in Hadoop's MapReduce so I was expecting it to be there in MongoDB. But the following does not seem to work:
var cache = {}; // Does not seem to work!
function () {
var hashValue = this.varValue1 + this.varValue2;
if(typeof(cache[hashValue])!= 'undefined') {
// Do nothing, we've processed at least one input record with this hash
} else {
// Process the input record
// Cache the record
cache[hashValue] = '1';
}
}
Is this not allowed in MongoDB's MapReduce implementation, or am I doing something wrong in JavaScript (not experienced in JS)?