Problem Statement
What is Map-Reduce in MongoDB and how does it compare to the Aggregation Framework?
Explanation
Map-Reduce is an older data processing paradigm in MongoDB that consists of two phases: the map phase processes each document and emits key-value pairs, and the reduce phase processes all emitted values for each key and outputs the final result. You write custom JavaScript functions for both phases.
In the map phase, you define a function that processes each input document and calls emit to output key-value pairs. In the reduce phase, you define a function that receives a key and array of all values emitted for that key, then returns a single result.
However, Map-Reduce is now deprecated and the Aggregation Framework is strongly recommended instead. The Aggregation Framework is much faster because it uses native code instead of JavaScript, has better optimization, and is easier to write and maintain.
Aggregation can accomplish everything Map-Reduce can do, but with better performance and clearer syntax. Use aggregation pipelines with stages like dollar group, dollar match, and dollar project instead of Map-Reduce. Only use Map-Reduce for very complex custom processing that cannot be expressed in the aggregation pipeline, but even then, consider using other solutions.
Code Solution
SolutionRead Only
// Old Map-Reduce approach (deprecated)
db.orders.mapReduce(
function() { emit(this.customerId, this.amount); },
function(key, values) { return Array.sum(values); },
{ out: "customer_totals" }
)
// Modern Aggregation approach (recommended)
db.orders.aggregate([
{
$group: {
_id: "$customerId",
total: { $sum: "$amount" }
}
},
{ $out: "customer_totals" }
])