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

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: 2.4.1
    • Component/s: MapReduce
    • None
    • Query Optimization
    • Minor Change
    • None
    • 3
    • None
    • 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.

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

                Created:
                Updated:
                Resolved: