query: { ts: { $gt: ISODate('2011-11-05 00:00:00') } }, out: { reduce: "session_stat" }, finalize: finalizeFunction } ); MongoDB Documentation, Release 3.0.4 The map function is a JavaScript function that associates or “maps” a value with a key and emits the key and value pair during a map-reduce (page 442) operation. To verify the key and value pairs emitted by the map function, write your own emit function. Consider a collection orders that contains documents of the following prototype: 7.3.5 Troubleshoot the Map Function { _id: ObjectId("50a8240b927d5d8b5891743c"), cust_id: "abc123", ord_date: new Date("Oct 04, 2012"), status: 'A', price: 250, items: [ { sku: "mmm", qty: 5, price: 2.5 }, { sku: "nnn", qty: 5, price: 2.5 } ] } 1. Define the map function that maps the price to the cust_id for each document and emits the cust_id and price pair: 2. Define the emit function to print the key and value: 3. Invoke the map function with a single document from the orders collection: 4. Verify the key and value pair is as you expected. 5. Invoke the map function with multiple documents from the orders collection: var myCursor = db.orders.find( { cust_id: "abc123" } ); while (myCursor.hasNext()) { var doc = myCursor.next(); print ("document _id= " + tojson(doc._id)); map.apply(doc); print(); } var emit = function (key, value) { print("emit"); print("key: " + key + " value: " + tojson(value)); } var map = function () { emit( this .cust_id, this .price); }; var myDoc = db.orders.findOne( { _id: ObjectId("50a8240b927d5d8b5891743c") } ); map.apply(myDoc); emit key: abc123 value:250 465 7.3. Aggregation Examples