MapReduce in MongoDB
MongoDB implements the MapReduce framework with its mapReduce()
command. An example is shown in Figure 11-6.
The first two statements define the JavaScript functions map1()
and reduce1()
. The third statement runs MapReduce on our library.books
collection (see Chapter 10, NoSQL Databases), applying those two functions, and naming the resulting collection "map_reduce_example"
. Appending the find()
command causes the output to be displayed.
The map1()
function emits the key-value pair (p, 1)
, where p
is the books.publisher
field. So this will generate 19 pairs, one for each books
document. For example, one of them will be ("OXF", 1)
. In fact, four of them will be ("MHE", 1)
, because there are four documents in the books
collection whose publisher
field is "MHE"
.
The reduce1()
function uses the Array.sum()
method to return the sum of the values of the second argument (numBooks
) for each value...