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.
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:
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!!
For map-reduce operations that require custom functionality, MongoDB provides the
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: https://github.com/Rishbah-76/MongoDB-Mapreduce