[SERVER-5168] Map Reduce Incorrectly Emitting null When a Constant Key is Used Created: 02/Mar/12  Updated: 15/Aug/12  Resolved: 07/Mar/12

Status: Closed
Project: Core Server
Component/s: JavaScript, MapReduce, Querying
Affects Version/s: 2.0.3
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Alexander Nagy Assignee: Unassigned
Resolution: Done Votes: 0
Labels: mapreduce,, mongos, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2K8R2 x64


Operating System: ALL
Participants:

 Description   

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,



 Comments   
Comment by Eliot Horowitz (Inactive) [ 07/Mar/12 ]

You can't emit from a reduce function.
You have to return it.

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