-
Type:
Improvement
-
Resolution: Done
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.4.1
-
Component/s: MapReduce
-
None
-
Query Optimization
-
Minor Change
-
None
-
0
-
None
-
None
-
None
-
None
-
None
-
None
Just was the case for reduce arguments in SERVER-8624, this in the emit function is immutable. This can produce unexpected results.
Small example code:
db.t.insert({test: 1}) function map() { this.test += 1; emit(this._id, this.test); } function reduce(key, values) { var r = {test: 0}; values.forEach(function (t) { r.test += t.test; }); return r; } var result = db.t.mapReduce(map, reduce,{ out: { inline: 1 }}); printjson(result);
The following code returns
{test:1}and "cannot write property test to read-only object" is written in mongodb.log. I expected
{test:2}in the value OR the write to this in the map to this to fail with an exception (just like the standard Object.freeze() would do). Just keeping the old value is confusing. The logged messages are hard to link to specific queries, since no context is provided in these messages, making issues related to this hard to debug.