I have a collection of real estates and I run map reduce on mongodb to calculate some basic statistic. Everything works fine locally, but when I run task on mongohq I get back recursive result.
Let me simplify things a bit, instead of real reduce fn, say reduce is:
function(key, values) {
return { values: values };
}
Map function:
function() {
emit('price', this.price);
}
When I run task locally, output looks like:
{
"values": [
1024.1712707182319,
661.0377201728149,
651.5957446808511,
1553.7073816617014,
1128.664171911323
]
}
Now fun part, when I run it on production db, output looks like:
{
"values": [
{
"values": [
{
"values": [
1561.5615615615618,
1026.2054507337525,
1428.5714285714287
]
},
1092.1177587844254,
1040.2010050251256,
1547.6190476190477
]
}
]
}
Any idea what could be wrong here?
The culprit is probably your reduce
function.
https://docs.mongodb.org/manual/reference/command/mapReduce/#dbcmd.mapReduce
the reduce function must be idempotent. Ensure that the following statement is true:
reduce( key, [ reduce(key, valuesArray) ] ) == reduce( key, valuesArray )
the reduce function should be commutative: that is, the order of the elements in the valuesArray should not affect the output of the reduce function, so that the following statement is true:
reduce( key, [ A, B ] ) == reduce( key, [ B, A ] )
Hope this helps,