Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-9357

make emit arguments mutable in MapReduce or fail if they're changed

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.1
    • Component/s: MapReduce
    • Labels:
      None
    • Query Optimization
    • Minor Change

      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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            markvandertol Mark van der Tol
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: