Map Reduce

Map Reduce

Map reduce in MongoDB is a data processing paradigm for condensing large volumes of data into usefull aggregated results. For map-reduce operations, MongoDB provides the mapReduce command. To use the mapReduce command we should provide two functions (Mapper and Reducer functions) and an object containing some fields (query, out, sort, limit).

Map Reduce syntax

db.coll.mapReduce(
    mapFunction,
    reduceFunction,
    { 
        query: <Query>
        out: "collection_where_to_output_result",
        sort: <document>
        limit: <number>
    }
)

Map Reduce parameters

  • mapFunction: Custom JavaScript function to map, or associate, values to a key. It output a field containing key and an array of values.
  • reduceFunction: Custom JavaScript function to reduce the values for the key to a single object.
  • query: A Query criteria that specifies the documents that we want to process using the mapReduce method.
  • out: The collection where to output our results
  • sort: Sort our data
  • limit: The number maximale of documents to be returned

Example of Map Reduce

In This example we will find the sum of amount of customers where the amount status is A. The result are explained in the following image

db.coll.mapReduce(
    function(){ emit(this.cust_id, this.amount);},
    function(key, values){ return Array.sum(values) },
    { 
        query: {status: "A"};
        out: "collection"
    }
)

Explaining the result of Map Reduce

Explaining Map and Reduce In MongoDB with Examples

For most aggregation operations, the Aggregation Pipeline provides better performance and more coherent interface. However, map-reduce operations provide some flexibility that is not presently available in the aggregation pipeline.