Ensure Reduce Function Idempotence 7.3.7 Additional Resources MongoDB Documentation, Release 3.0.4 Because the map-reduce operation may call a reduce multiple times for the same key, and won’t call a reduce for single instances of a key in the working set, the reduce function must return a value of the same type as the value emitted from the map function. You can test that the reduce function process “reduced” values without affecting the final value. 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; }; 2. Define a sample key: 3. Define a sample valuesIdempotent array that contains an element that is a call to the reduceFunction2 function: var myKey = 'myKey'; 1. 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 valuesIdempotent = [ { count: 1, qty: 5 }, { count: 2, qty: 10 }, reduceFunction2(myKey, [ { count:3, qty: 15 } ] ) ]; var values1 = [ { count: 1, qty: 5 }, { count: 2, qty: 10 }, { count: 3, qty: 15 } ]; reduceFunction2(myKey, valuesIdempotent); reduceFunction2(myKey, values1); { "count" : 6, "qty" : 30 } 4. Define a sample values1 array that combines the values passed to reduceFunction2 : 5. Invoke the reduceFunction2 first with myKey and valuesIdempotent and then with myKey and values1 : 6. Verify the reduceFunction2 returned the same result: • MongoDB Analytics: Learn Aggregation by Example: Exploratory Analytics and Visualization Using Flight Data 15 Chapter 7. Aggregation 468 $^{15}$http://www.mongodb.com/presentations/mongodb-analytics-learn-aggregation-example-exploratory-analytics-and-visualization?jmp=docs