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

Map Reduce Incorrectly Emitting null When a Constant Key is Used

    • ALL

      Running MapReduce with a constant key causes null to always be emitted. This makes any scenario where you need to perform global aggregation on the server impossible. While the repro code below is trivial, our actual production code performs much more sophisticated analysis logic on objects.

      The following repro code below will assert in the reduce function:

      db.items.mapReduce(function () {
      emit("foo",

      { a: 1 }

      );
      }, function (key, values) {
      var sum = 0;
      values.forEach(function (value)

      { sum += value.a; });
      emit(key, { a: sum });
      }, { out: "foo_out" });

      "assertion" : "invoke failed: JS Error: TypeError: value has no properties nofile_b:2",
      "assertionCode" : 9004,
      "errmsg" : "db assertion failure",
      "ok" : 0

      Note that just a simple change in the id causes the failure to go away (this is not functionally equivalent, nor is it a workaround, but it illustrates the bug):

      db.items.mapReduce(function () {
      emit(this._id, { a: 1 });
      }, function (key, values) {
      var sum = 0;
      values.forEach(function (value) { sum += value.a; }

      );
      emit(key,

      { a: sum }

      );
      },

      { out: "foo" }

      );

      "result" : "foo",
      "timeMillis" : 7951,
      "counts" :

      { "input" : 83338, "emit" : 83338, "reduce" : 0, "output" : 83338 }

      ,
      "ok" : 1,

            Assignee:
            Unassigned Unassigned
            Reporter:
            optimiz3 Alexander Nagy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: