Tutorial for Averaging Over a field MongoDB using MapReduce

Mapping Function

The Mapping Function is below:

Map Function

Some things to note about the above method is that it returns a string. It does this because the mapReduceObject takes a Java Command because mongoDB works with JavaScript. The above code inside the string is actually JavaScript and is formatted and spaced to be readable. The code creates a total pages variable and sets it to the number of pages in the current book. Fill out the method in the project called mapAverage() with the code above.

Reduce Function

The reducing function is below. Use the code below to finish the reduceAverage() method:

Map Function

This code above is for a JavaScript function that creates a variable n that stores to values the total pages and num. Total pages is the total pages that are in the whole DBCollection of books and the num is the total number of books in the DBCollection. The for loop goes through the collection of books incrementing the totalPages by the number of pages in each book and the incrementing num by 1 for each book in the collection. The function will then return the variable n.

Next Page of Tutorial