MongoDB Aggregation Framework with Mapper-Reducer Program

What is Aggregation Framework ?

What is Map Reduce ?

db.orders.insertMany([   { _id: 1, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-01"), price: 25, items: [ { sku: "oranges", qty: 5, price: 2.5 }, { sku: "apples", qty: 5, price: 2.5 } ], status: "A" },   { _id: 2, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-08"), price: 70, items: [ { sku: "oranges", qty: 8, price: 2.5 }, { sku: "chocolates", qty: 5, price: 10 } ], status: "A" },   { _id: 3, cust_id: "Busby Bee", ord_date: new Date("2020-03-08"), price: 50, items: [ { sku: "oranges", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "A" },   { _id: 4, cust_id: "Busby Bee", ord_date: new Date("2020-03-18"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" },   { _id: 5, cust_id: "Busby Bee", ord_date: new Date("2020-03-19"), price: 50, items: [ { sku: "chocolates", qty: 5, price: 10 } ], status: "A"},   { _id: 6, cust_id: "Cam Elot", ord_date: new Date("2020-03-19"), price: 35, items: [ { sku: "carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" },   { _id: 7, cust_id: "Cam Elot", ord_date: new Date("2020-03-20"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" },   { _id: 8, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 75, items: [ { sku: "chocolates", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" },   { _id: 9, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 55, items: [ { sku: "carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" },   { _id: 10, cust_id: "Don Quis", ord_date: new Date("2020-03-23"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }])

Return the Total Price Per Customer

  1. Define the map function to process each input document:
  • In the function, this refers to the document that the map-reduce operation is processing.
  • The function maps the price to the cust_id for each document and emits the cust_id and price.
var mapFunction1 = function() {
emit(this.cust_id, this.price);
};
  • The valuesPrices is an array whose elements are the price values emitted by the map function and grouped by keyCustId.
  • The function reduces the valuesPrice array to the sum of its elements.
var reduceFunction1 = function(keyCustId, valuesPrices) {   return Array.sum(valuesPrices);};
db.orders.mapReduce(   mapFunction1,   reduceFunction1,   { out: "map_reduce_example" })
db.map_reduce_example.find().sort( { _id: 1 } 
{ "_id" : "Ant O. Knee", "value" : 95 }{ "_id" : "Busby Bee", "value" : 125 }{ "_id" : "Cam Elot", "value" : 60 }{ "_id" : "Don Quis", "value" : 155 }

Aggregation Alternative:

db.orders.aggregate([   { $group: { _id: "$cust_id", value: { $sum: "$price" } } },   { $out: "agg_alternative_1" }])
  1. The $group stage groups by the cust_id and calculates the value field (See also $sum). The value field contains the total price for each cust_id.
  2. The stage output the following documents to the next stage:
  • { "_id" : "Don Quis", "value" : 155 }{ "_id" : "Ant O. Knee", "value" : 95 }{ "_id" : "Cam Elot", "value" : 60 }{ "_id" : "Busby Bee", "value" : 125 }
  • db.agg_alternative_1.find().sort( { _id: 1 } )
{ "_id" : "Ant O. Knee", "value" : 95 }{ "_id" : "Busby Bee", "value" : 125 }{ "_id" : "Cam Elot", "value" : 60 }{ "_id" : "Don Quis", "value" : 155 }

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jyoti Pawar

Jyoti Pawar

Devops || AWS || ML || Deep learning || Python || Flask || Ansible RH294 || OpenShift DO180