On this page本页内容
The reduce function is a JavaScript function that “reduces” to a single object all the values associated with a particular key during a map-reduce operation. The reduce function must meet various requirements. This tutorial helps verify that the reduce function meets the following criteria:
reduce function must return an object whose type must be identical to the type of the value emitted by the map function.valuesArray should not affect the output of the reduce function.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().
Note
Starting in MongoDB 4.4, mapReduce no longer supports the deprecated BSON type JavaScript code with scope (BSON type 15) for its functions. The map, reduce, and finalize functions must be either BSON type String (BSON type 2) or BSON type JavaScript (BSON type 13). To pass constant values which will be accessible in the map, reduce, and finalize functions, use the scope parameter.
The use of JavaScript code with scope for the mapReduce functions has been deprecated since version 4.2.1.
You can test that the reduce function returns a value that is the same type as the value emitted from the map function.
reduceFunction1 function that takes the arguments keyCustId and valuesPrices. valuesPrices is an array of integers:
reduceFunction1 with myTestValues:
reduceFunction1 returned an integer:
reduceFunction2 function that takes the arguments keySKU and valuesCountObjects. valuesCountObjects is an array of documents that contain two fields count and qty:
reduceFunction2 with myTestObjects:
reduceFunction2 returned a document with exactly the count and the qty field:
The reduce function takes a key and a values array as its argument. You can test that the result of the reduce function does not depend on the order of the elements in the values array.
values1 array and a sample values2 array that only differ in the order of the array elements:
reduceFunction2 function that takes the arguments keySKU and valuesCountObjects. valuesCountObjects is an array of documents that contain two fields count and qty:
reduceFunction2 first with values1 and then with values2:
reduceFunction2 returned the same result:
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.
reduceFunction2 function that takes the arguments keySKU and valuesCountObjects. valuesCountObjects is an array of documents that contain two fields count and qty:
valuesIdempotent array that contains an element that is a call to the reduceFunction2 function:
values1 array that combines the values passed to reduceFunction2:
reduceFunction2 first with myKey and valuesIdempotent and then with myKey and values1:
reduceFunction2 returned the same result: