Reputation: 8968
I know this has been asked countless times on the internet but I can't figure out what's going on here and I have been banging my head against walls for hours now.
This works :
in myscript.js :
obj = 'hello';
var f = function() {
printjson('obj=' + obj);
}
f();
$ mongo myscript.js
obj=hello
This doesn't work :
date1 = "2013-09-03T00:00:00Z";
date2 = "2013-09-04T00:00:00Z";
var mapIntensities = function() {
emit(this.unit, this.intensity);
};
var reduceIntensities = function(unit, intensities) {
return {date: date1, "unit": unit, "intensity": Array.sum(intensities)};
};
db.units.mapReduce(mapIntensities, reduceIntensities, {out: "aggregate_intensities", query: {"DATE": {$gte: ISODate(date1), $lt: ISODate(date2)}}})
Why is that ? The problem occurs in the reduce() function (if just there I replace date1 with the hardcoded value it works)
Is it specific to mongodb's way to do mapreduce ? (as the working example suggests)
Upvotes: 2
Views: 1420
Reputation: 43884
Because date1
is never defined within the Map Reduce itself, it is only ever used here:
return {date: date1, "unit": unit, "intensity": Array.sum(intensities)};
And then you do not define a variable to input at all into the actual call of the Map Reduce.
The Map Reduce runs within a self contained JavaScript (spidermonkey or v8) envo within MongoDB, not in the mongo console as I assume you are doing there.
You need to use the scope
param: http://docs.mongodb.org/manual/reference/command/mapReduce/#dbcmd.mapReduce to send in date1
Upvotes: 1