Grouping and aggregation
In the following example we will perform grouping and aggregation in order to get statistics (sum
, max
, min
, and avg
) about NBA players and their number of points scored. First, the map
function will send the name of the player
and the number of points
scored for each game. The map
function will look similar to the following code:
function(){emit(this.player, this.points); }
Then, we can perform all the aggregation functions simultaneously using the method sum
from the JavaScript Array
object and the max
/min
functions of the JavaScript Math
object. The reduce
function will look similar to the following code:
function(key, values) { var explain = {total:Array.sum(values), max:Math.max.apply(Math, values), min:Math.min.apply(Math, values), avg:Array.sum(values)/values.length} return explain }
For this example we will create synthetic data, randomly mixing the name of the 10 players and we will assign a random score between 0 and 100. Then, we will insert...