I have this a mongo collection like this:
{
"_id" : ObjectId("4fd930ecd729f5b31ea5ad03"),
"O_ORDERKEY" : NumberLong(359),
"O_CUSTKEY" : {
"$ref" : "CUSTOMER",
"$id" : ObjectId("4f973ff37d6517e9723c4d63")
},
"O_ORDERSTATUS" : "F",
"O_TOTALPRICE" : 239998.53,
"O_ORDERDATE" : ISODate("1994-12-19T02:00:00Z"),
"O_ORDERPRIORITY" : "3-MEDIUM",
"O_CLERK" : "Clerk#000000934",
"O_SHIPPRIORITY" : 0,
"O_COMMENT" : "furiously final foxes are. regular,"
}
And i want to sum all O_TOTALPRICE when O_SHIPPRIORITY = 0 then i use this command:
db.runCommand({
mapreduce: "ORDERS7",
query: {
O_SHIPPRIORITY: 0
},
map : function Map() {
emit("sum",this.O_TOTALPRICE);
},
reduce : function Reduce(key, values) {
var sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
},
out: 'query'
});
db.query.find();
but my result was this:
{ "_id" : "sum", "value" : null }
Whats wrong why value is null?
The following works for me, indicating that your map-reduce command is fine as described.
Please try the following, exactly as written and see if you get the same result.
If you do, then your problem is elsewhere, not anything described in your question.
Hope that this helps.
orders7.js
$ mongo xyzzy orders7.js