Using MapReduce with MongoDB
MongoDB provides us with a mapReduce
command and in the following figure, we can observe the life circle of the MapReduce process in MongoDB. We start with a Collection or a query and each document in the collection will call the map
function. Then, using the emit
function we will create an intermediate hash map (See the following figure) with a list of pairs (key-value). Next, the reduce
function will iterate the intermediate hash map and it will apply some operation to all values of each key. Finally, the process will create a brand new collection with the output. The map/reduce functions in MongoDB will be programmed with JavaScript.
Tip
You can find the reference documentation of MapReduce with MongoDB at http://docs.mongodb.org/manual/core/map-reduce/.
The map function
The
map
function will call the emit
function one or more times (See the next figure). We can access all the attributes of each document in the collection with the this
keyword. The intermediate...