Geek Culture
Published in

Geek Culture

How To Perform Map Reduce In Mongo DB

Hey, guys hope you are doing in today's article we are going to perform sorting of data that is present in our mongo DB cluster so we can do this in many ways but today we are going to use map-reduce for sorting data that is present in our mongo DB cluster.

In map-reduce we make two functions one for mapping the data nowhere mapping refers to suppose you have 1000 documents(in mongo document refer to each data we get from the user) and suppose in that document we have many customers with same customer id but they buy a new product every time and a new entry is created every time customer purchase customer now we want to make one list that is named with customer id and inside that they have all price that is spent by that customer-id and once we get that list of price for each customer we want to perform addition(its depend on you what you want to with that function we just have to write function) so here wee write our reduce function that processes our map data and give final output we want.

I hope you get an idea of what is map-reduce program is now let's proceed for practical for better understanding.

First, let's create a sample collection with name orders in whatever database you want from the below command.

use [database name]
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” }])

The order is just a collection name in the above command you can change it to whatever name you want.

Some helpful commands for general operation in Mongo DB.

use dbname to change dbname
show collections to get collection in db
db.collectionnaame.find()=> To see all data of collection

Now we are ready to make mapper function for mapping data.

var mapFunction1 = function() {emit(this.cust_id, this.price);};

In the above mapper function, this refers to the address of a particular document and emit take key: value pair to group item in the collection in above command cust_id is our key and price is value. After running the command in the backend your data will look like {“cust_id”:[price1, price 2, etc]} for each customer.

Now we can make we make our reducer function in this we make a function that takes two arguments cust_id and price that we make in our mapper function and return some of all the elements in the array.

var reduceFunction1 = function(keyCustId, valuesPrices) {return Array.sum(valuesPrices);};

Now we can call this function on which our collection we want from the below command.

db.[collection name].mapReduce([mapfunction name],[mapfunction name],{ out: [any collection name you want output want to be save] })Eg:-db.orders.mapReduce(mapFunction1,reduceFunction1,{ out: “map_reduce_example” })

If want to know more about the above example can see the mongo official site link is below.

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Open Closed Principle

Building a personal homepage in 2 days for beginners (1)

API gateway vs Microgateway (Microservices gateway)

Steam Next Fest オンライン合宿 / Steam Next Fest Online Camp

What does coding look like to you in your mind?

Run WordPress on Azure Kubernetes Service (AKS) in just 8 commands

RedX Review: Why I Left RedX But Came Back

Why your company/industry needs to adopt agile?

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
Gupta Aditya

Gupta Aditya

More from Medium

Normalization in DBMS

NoSQL, MongoDB, HiveQL

Posgres Concurrency Notes: 1

Why Edgar Codd came up with the Relational Model