0% found this document useful (0 votes)
55 views3 pages

Assignment Group B16

Map reduce is a data processing paradigm used by MongoDB to condense large datasets into aggregated results. It works by mapping input documents to emit key-value pairs, then reducing by each unique key. For example, a map reduce operation was performed on a MongoDB collection to sum the amounts for each customer ID where the status was "A", outputting the results to a new collection. This allows condensing transactional data by customer into total amounts in a single pass over the data.

Uploaded by

mayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

Assignment Group B16

Map reduce is a data processing paradigm used by MongoDB to condense large datasets into aggregated results. It works by mapping input documents to emit key-value pairs, then reducing by each unique key. For example, a map reduce operation was performed on a MongoDB collection to sum the amounts for each customer ID where the status was "A", outputting the results to a new collection. This allows condensing transactional data by customer into total amounts in a single pass over the data.

Uploaded by

mayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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