MongoDB Documentation, Release 3.0.4 See also: 7.3.6 Troubleshoot the Reduce Function Confirm Output Type Chapter 7. Aggregation 466 The map function must meet various requirements. For a list of all the requirements for the map function, see mapReduce , or the mongo shell helper method db.collection.mapReduce() . The reduce function is a JavaScript function that “reduces” to a single object all the values associated with a par- ticular key during a map-reduce (page 442) operation. The reduce function must meet various requirements. This tutorial helps verify that the reduce function meets the following criteria: For a list of all the requirements for the reduce function, see mapReduce , or the mongo shell helper method db.collection.mapReduce() . You can test that the reduce function returns a value that is the same type as the value emitted from the map function. 6. Verify the key and value pairs are as you expected. • The reduce function must return an object whose type must be identical to the type of the value emitted by the map function. • The order of the elements in the valuesArray should not affect the output of the reduce function. • The reduce function must be idempotent . 1. Define a reduceFunction1 function that takes the arguments keyCustId and valuesPrices . valuesPrices is an array of integers: 2. Define a sample array of integers: 3. Invoke the reduceFunction1 with myTestValues : 4. Verify the reduceFunction1 returned an integer: 5. Define a reduceFunction2 function that takes the arguments keySKU and valuesCountObjects . valuesCountObjects is an array of documents that contain two fields count and qty : 20 var reduceFunction2 = function (keySKU, valuesCountObjects) { reducedValue = { count: 0, qty: 0 }; for ( var idx = 0; idx < valuesCountObjects.length; idx++) { reducedValue.count += valuesCountObjects[idx].count; reducedValue.qty += valuesCountObjects[idx].qty; } return reducedValue; }; var reduceFunction1 = function (keyCustId, valuesPrices) { return Array.sum(valuesPrices); }; var myTestValues = [ 5, 5, 10 ]; reduceFunction1('myKey', myTestValues);