Professional Documents
Culture Documents
Advance
MapReduce with MongoDB
• Syntax: db.collection-name.mapReduce(mapFunction,
reduceFunction, options)
MapReduce Operation
db.testMR.mapReduce(mapFunction1,
reduceFunction1,
{ out: "map_reduce_example" }
)
Example
• Collection contains documents, document as shown
below
{
cust_id: "abc123",
status: 'A',
price: 25,
emit(this.cust_id, this.price);
};
return Array.sum(valuesPrices);
};
Perform the MapReduce
db.orders.mapReduce(
mapFunction1,
reduceFunction1,
{ out: “sum_prices_per_customer” }
• sort
• limit
Example
sample doc
{
title: "Anthropoid",
year: 2016,
actors: [ ObjectId("8") ]
}
Example
• count number of movies per year, starting from 2005, and
sort
sample doc
{
title: "Anthropoid",
year: 2016,
actors: [ ObjectId("8") ]
}
Index
• If data is not indexed, this means that the DB will scan the entire
collection to find docs based on given conditions
• created automatically
• created manually
Index types
• Default: _id
• Single field
• Compound fields
• multikey index
• Hashed
• Text
• Master/slave replication
• using the indexed key that is immutable (for example the _id)
• In the background
• Query functionality
• CRUD operations
• MapReduce
• index structures