Professional Documents
Culture Documents
Assignment Group B16
Assignment Group B16
Assignment Group B16
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.