ADB - Lab Sheet 4
ADB - Lab Sheet 4
Map Reduce
Map reduce is used for aggregating results for the large volume of data. Map
reduce has two main functions one is a map that groups all the documents and
the second one is the reduce which performs operation on the grouped data.
Syntax:
db.collectionName.mapReduce(mappingFunction, reduceFunction, {out:'Result'});
Example:
In the following example, we are working with:
Database: GeeksForGeeks
Collection: studentsMark
Documents: Seven documents that contain the details of the students in the form
of field-value pairs.
var mapfunction = function(){emit(this.age, this.marks)}
var reducefunction = function(key, values){return Array.sum(values)}
db.studentsMarks.mapReduce(mapfunction, reducefunction, {'out':'Result'})
Now, we will group the documents on the basis of age and find total marks in
each age group. So, we will create two variables first mapfunction which will
emit age as a key (expressed as “_id” in the output) and marks as value this
emitted data is passed to our reducefunction, which takes key and value as
grouped data, and then it performs operations over it. After performing
reduction the results are stored in a collection here in this case the collection is
Results.