Assignment Group B16

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Assignment-16(Group-B)

Title: Map reduce operation


Aim: Map reduces operation with suitable example using MongoDB.
Prerequisites: Basic of MongoDB.
Objectives: Ability of problem solving using advanced databases techniques and tools
Theory:
Theory:
MongoDB Map Reduce:
As per the MongoDB documentation, Map-reduce is a data processing paradigm for
condensing large volumes of data into useful aggregated results. MongoDB uses
mapReduce command for map-reduce operations. MapReduce is generally used for
processing large data sets.
The map-reduce function first queries the collection, then maps the result documents to
emit key-value pairs which is then reduced based on the keys that have multiple values.
map is a javascript function that maps a value with a key and emits a key-valur pair

reduce is a javscript function that reduces or groups all the documents having the same
key
out specifies the location of the map-reduce query result
query specifies the optional selection criteria for selecting documents
sort specifies the optional sort criteria
limit specifies the optional maximum number of documents to be returned
MapReduce Command:
Following is the syntax of the basic mapReduce command:
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, //reduce function
{
out: collection,
query: document,
sort: document,
limit: number
}
)

Map-reduce is a data processing paradigm for condensing large volumes of data into
useful aggregatedresults. For map-reduce operations, MongoDB provides the mapReduce database
command.
Consider the following map-reduce operation:

> db.mycol1.insert({"cust_id":"A123","amount":500,"status":"A"})
WriteResult({ "nInserted" : 1 })
> db.mycol1.insert({"cust_id":"A123","amount":250,"status":"A"})
WriteResult({ "nInserted" : 1 })
> db.mycol1.insert({"cust_id":"B212","amount":200,"status":"A"})
WriteResult({ "nInserted" : 1 })
> db.mycol1.insert({"cust_id":"A123","amount":300,"status":"D"})
WriteResult({ "nInserted" : 1 })

> db.mycol1.find().pretty()
{
"_id" : ObjectId("57d7edd4b4e4d1fdf868caf9"),
"cust_id" : "A123",
"amount" : 500,
"status" : "A"
}
{
"_id" : ObjectId("57d7ede7b4e4d1fdf868cafa"),
"cust_id" : "A123",
"amount" : 250,
"status" : "A"
}
{
"_id" : ObjectId("57d7ee0db4e4d1fdf868cafb"),
"cust_id" : "B212",
"amount" : 200,
"status" : "A"
}
{
"_id" : ObjectId("57d7ee21b4e4d1fdf868cafc"),
"cust_id" : "A123",
"amount" : 300,
"status" : "D"
}
> db.mycol1.mapReduce(
... function(){emit(this.cust_id,this.amount);},
... function(key,values){return Array.sum(values)},
... {
... query:{status:"A"},
... out:"orders_total"}).find().pretty()
{ "_id" : "A123", "value" : 750 }
{ "_id" : "B212", "value" : 200 }
>

Conclusion:
Here we performed Map reduce operation with suitable example using MongoDB.

You might also like