6. Verify the key and value pairs are as you expected. 

## See also: 

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() . 

## 7.3.6 Troubleshoot the Reduce Function 

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: 

• 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 . 

For a list of all the requirements for the reduce function, see mapReduce , or the mongo shell helper method db.collection.mapReduce() . 

## Confirm Output Type 

You can test that the reduce function returns a value that is the same type as the value emitted from the map function. 

1. Define a reduceFunction1 function that takes the arguments keyCustId and valuesPrices . valuesPrices is an array of integers: 

var reduceFunction1 = function (keyCustId, valuesPrices) { return Array.sum(valuesPrices); }; 

2. Define a sample array of integers: 

var myTestValues = [ 5, 5, 10 ]; 

3. Invoke the reduceFunction1 with myTestValues : 

reduceFunction1('myKey', myTestValues); 

4. Verify the reduceFunction1 returned an integer: 

20 

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 : 

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; }; 

