[SERVER-9357] make emit arguments mutable in MapReduce or fail if they're changed Created: 16/Apr/13  Updated: 06/Dec/22  Resolved: 04/Feb/22

Status: Closed
Project: Core Server
Component/s: MapReduce
Affects Version/s: 2.4.1
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Mark van der Tol Assignee: Backlog - Query Optimization
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Backwards Compatibility: Minor Change
Participants:

 Description   

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.



 Comments   
Comment by Esha Bhargava [ 04/Feb/22 ]

Closing these tickets as part of the deprecation of mapReduce.

Generated at Thu Feb 08 03:20:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.