Using Map-Reduce in MongoDB

What is MongoDB?

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

What is Map-Reduce?

Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. To perform map-reduce operations, MongoDB provides the mapReduce database command.

Consider the following map-reduce operation:

In this map-reduce operation, MongoDB applies the map phase to each input document (i.e. the documents in the collection that match the query condition). The map function emits key-value pairs. For those keys that have multiple values, MongoDB applies the reduce phase, which collects and condenses the aggregated data. MongoDB then stores the results in a collection. Optionally, the output of the reduce function may pass through a finalize function to further condense or process the results of the aggregation.

All map-reduce functions in MongoDB are JavaScript and run within the mongod process. Map-reduce operations take the documents of a single collection as the input and can perform any arbitrary sorting and limiting before beginning the map stage. mapReduce can return the results of a map-reduce operation as a document, or may write the results to collections.

Practical Use Case:

In today’s practical we will write a custom Javascript Map-Reduce function where we would count the number of male candidates in each state.

Now, this is a peek at the data provided to us. The ‘state’ is an embedded document under ‘location’.

Now we have written a custom mapper function that will pass state as the key and will map 1 in front of each state.

The reducer function will count all the states with the same name.

The mapReduce function will combine the mapper function and the reducer function and store the output document under the name ‘result’.

We can clearly see the output has been produced in the below screenshot.

Now we are Using: Aggregation Pipeline as Alternative to Map-Reduce!!

An aggregation pipeline provides better performance and usability than a map-reduce operation.

Map-reduce operations can be rewritten using aggregation pipeline operators, such as $group, $merge, and others.

For map-reduce operations that require custom functionality, MongoDB provides the $accumulator and $function aggregation operators starting in version 4.4. Use these operators to define custom aggregation expressions in JavaScript.

Now we have created the alternative of the above MapReduce function below in the aggregate alternative.

Now you can view the above code in the GitHub link provided below.

Github Link:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Student from 2nd Year, A proud ARTH learner, love new technologies, Curious about many thing, likes to explore places, love eating pizza and much more.