Mongodb Map Reduce Examples in Mongodb
Mongodb Map Reduce Examples in Mongodb
By Prof. B.A.Khivsara
Note: The material to prepare this presentation has been taken from internet and
are generated only for students reference and not for commercial use.
Map-Reduce
• Map-reduce is a data processing paradigm for condensing
large volumes of data into useful aggregated results. For map-
reduce operations, MongoDB provides the mapReduce
database command.
• Consider the following map-reduce operation:
Map-Reduce
Map-Reduce
And then this key value pair is fed into a Reducer, which
will process the values.
Map-Reduce Syntax
db.collection.mapReduce(
function() {emit(key, value);},
//Define map function
function(key,values) {return reduceFunction}, {
//Define reduce function
out: collection,
query: document,
sort: document,
limit: number
}
)
Map-Reduce Syntax Explanation
• The above map-reduce function will query the collection, and then map
the output documents to the emit key-value pairs. After this, it is
reduced based on the keys that have multiple values. Here, we have
used the following functions and parameters.
• > db.author.save({
"book_title" : "MongoDB Tutorial", "author_name" : "aparajita",
"status" : "active", "publish_year": "2016" })
• > db.author.save({
"book_title" : "Software Testing Tutorial", "author_name" : "aparajita",
"status" : "active", "publish_year": "2015" })
• > db.author.save({
"book_title" : "Node.js Tutorial", "author_name" : “Kritika",
"status" : "active", "publish_year": "2016" })
• > db.author.save({
"book_title" : "PHP7 Tutorial", "author_name" : "aparajita",
"status" : “passive", "publish_year": "2016" })
Example1 MapReduce function
db.author.find()
• Out-Put
{ "_id" : "aparajita", "value" : 2 }
{ "_id" : “Kritika", "value" : 1 }
Code:
db.author.mapReduce(
function() { emit(this.author_name,1); },
function(key, values) {return Array.sum(values)}, {
query: { status : "active" },
out: "author_total” })
Example2 MapReduce function
• Consider the following document structure of Students
• > db.stud.mapReduce(
function(){ emit(this.Name,1)},
function(key, values) {return Array.sum(values)},
{out: “Name_Total“ }).find()
• > db.stud.mapReduce(
function() { emit(this.Name,this.Marks) },
function(key, values) {return Array.sum(values)},
{out: “Total_Marks“ }).find()
Example2 MapReduce function
• > db.stud.mapReduce(
... function(){ emit(this.Name,this.Marks)},
... function(key, values) {return Array.sum(values)},
... {out: 'Name_Total'}).find().sort({value:1})
• db.stud.mapReduce(
... function(){ emit(this.Name,this.Marks)},
... function(key, values) {return Array.sum(values)},
... {out: 'Name_Total'}).find().limit(1)
• db.stud.mapReduce(
function(){ emit(this.Name,1)},
function(key, values) {return Array.sum(values)},
{query:{Marks:{$gt:70}},out: 'Name_Total'}).find()
References
• https://docs.mongodb.com/manual/core/map-reduce/
• https://www.eduonix.com/blog/web-programming-
tutorials/learn-mapreduce-command-mongodb/
• https://www.tutorialspoint.com/mongodb/mongodb_map_re
duce.htm