Ideas behind MongoDB Map / Reduce troubleshooting see here
Enter source data below:
[ { _id: ObjectId("50a8240b927d5d8b5891743a"), cust_id: "abc123", ord_date: new Date("Oct 04, 2012"), price: 10 }, { _id: ObjectId("50a8240b927d5d8b5891743b"), cust_id: "abc123", ord_date: new Date("Oct 05, 2012"), price: 20 }, { _id: ObjectId("50a8240b927d5d8b5891743c"), cust_id: "efj456", ord_date: new Date("Oct 04, 2012"), price: 99, } ]
Write your Map / Reduce / Finalize (optional) code:
For a step-by-step debug - put a "debugger" instruction to the code (browser Dev Tool must be opened)
{ map: function() { emit({ customer: this.cust_id, month: this.ord_date.getUTCMonth() }, this.price); }, reduce: function(key, values) { return Array.sum(values); }, finalize: function(key, reduced) { return reduced; } }
See aggregation results here:
{ _id: 1, value: 22 }