MapReduce example

use shop

// Insert orders
db.orders.insertMany([
{orderId:1, customer:”Mital”, product:”Laptop”, price:50000, quantity:1},
{orderId:2, customer:”Mital”, product:”Mouse”, price:1000, quantity:2},
{orderId:3, customer:”Rahul”, product:”Keyboard”, price:2000, quantity:1},
{orderId:4, customer:”Rahul”, product:”Monitor”, price:12000, quantity:2},
{orderId:5, customer:”Priya”, product:”Mobile”, price:25000, quantity:1},
{orderId:6, customer:”Mital”, product:”Pen Drive”, price:800, quantity:3}
]);

// Map function
var mapFunction = function() {

var total = this.price * this.quantity;

emit(this.customer, total);

};

// Reduce function
var reduceFunction = function(customer, amounts) {

return Array.sum(amounts);

};

// MapReduce
db.orders.mapReduce(
mapFunction,
reduceFunction,
{
out: “customer_total_orders”
}
);

// Display result
db.customer_total_orders.find().pretty();

MongoDB MapReduce example using customer orders, where we group orders by customer and calculate the total order price.

1. Create the orders collection

use shop

db.orders.insertMany([
    {
        orderId: 1,
        customer: "Mital",
        product: "Laptop",
        price: 50000,
        quantity: 1
    },
    {
        orderId: 2,
        customer: "Mital",
        product: "Mouse",
        price: 1000,
        quantity: 2
    },
    {
        orderId: 3,
        customer: "Rahul",
        product: "Keyboard",
        price: 2000,
        quantity: 1
    },
    {
        orderId: 4,
        customer: "Rahul",
        product: "Monitor",
        price: 12000,
        quantity: 2
    },
    {
        orderId: 5,
        customer: "Priya",
        product: "Mobile",
        price: 25000,
        quantity: 1
    },
    {
        orderId: 6,
        customer: "Mital",
        product: "Pen Drive",
        price: 800,
        quantity: 3
    }
])

Here:

Order Amount = price × quantity

For example:

Mouse = 1000 × 2 = 2000

2. Create the Map Function

The map function takes each order and emits:

customer → order amount
var mapFunction = function() {

    var total = this.price * this.quantity;

    emit(this.customer, total);
};

For example, the map function produces:

Mital → 50000
Mital → 2000
Rahul → 2000
Rahul → 24000
Priya → 25000
Mital → 2400

3. Create the Reduce Function

The reduce function adds all amounts belonging to the same customer.

var reduceFunction = function(customer, amounts) {

    return Array.sum(amounts);
};

For example:

Mital → [50000, 2000, 2400]

50000 + 2000 + 2400
= 54400

4. Execute MapReduce

db.orders.mapReduce(
    mapFunction,
    reduceFunction,
    {
        out: "customer_total_orders"
    }
);

This creates a new collection:

customer_total_orders

5. Display the Result

db.customer_total_orders.find().pretty()

Expected Output

{
    "_id" : "Mital",
    "value" : 54400
}
{
    "_id" : "Priya",
    "value" : 25000
}
{
    "_id" : "Rahul",
    "value" : 26000
}

Calculation

CustomerOrdersTotal
Mital50000 + 2000 + 240054400
Rahul2000 + 2400026000
Priya2500025000

By admin