Map-reduce is a data processing program (paradigm) that considers large data to get useful aggregation results. For map-reduce operations, MongoDB provides the MapReduce command.
Consider the following map-reduce operation:
In this map-reduce operation, MongoDB applies a map operation to each input document (for example, a document in the collection that satisfies the query criteria). The map function enters a key-value pair. For those keys that have multiple values, MongoDB uses the reduce phase, which collects and compresses the aggregated data. Mongo-db then the result exists in a set. The output of the reduce function can optionally be passed to a finalize function to further compress or process the aggregated result.
In MongoDB, all the Map-reduce functions are JavaScript code, all running in the Mongod process. The Map-reduce operation accepts a collection of documents as input and can perform arbitrary sorting and throttling before the map stage. MapReduce can return the result of a map-reduce operation as a document, or it may write a result to the collection. The collection of inputs and outputs may be shared.
Note:
For most aggregation operations, the aggregation pipeline provides better performance and a more consistent interface. However, the Map-reduce operation provides the flexibility that is not available in the aggregation pipeline.
Map-reduce JavaScript functions
The mongodb,map-reduce operation uses a custom function to go to map, or to associate a key with a value. If a key has more than one value and it corresponds, the reduce operation "operation" the value of the key to a single object (the reduces of the values for the key to a).
The custom JavaScript function gives map-reduce flexibility. For example, when working with a document, the map function produces more than one key-value pair to match or no key-value pair to match. The Map-reduce function can also use a custom JavaScript function to make final changes to the results at the end of the map and reduce function operations.
Map-reduce behavior
The Mongodb,map-reduce function is able to write results or return results to the collection online. If you write the output of Map-reduce to a collection, you can perform subsequent map-reduce operations on the same input collection, merging overrides, merging, or cutting the previous results.
When the results of the map-reduce operation are returned online, the resulting document must be within the Bson document size limit, which is currently 16M.
MongoDB supports map-reduce operations on shared collections and can also output results to shared collections.