MongoDB Aggregation Framework and Map-Reduce

Aakash Choudhary
4 min readSep 30, 2021
Map Reduce on MongoDB

Don’t know what aggregation Framework and what MongoDB is,

then this article is cooked and served for you.

What are Databases?

A database is an organized collection of structured information, or data, typically stored electronically in a computer system. A database is usually controlled by a database management system(DBMS). Together, the data and the DBMS, along with the applications that are associated with them, are referred to as a database system, often shortened to just database.

What is MongoDB?

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables.

The MongoDB database is developed and managed by MongoDB.Inc under SSPL(Server Side Public License) and initially released in February 2009. It also provides official driver support for all the popular languages like C, C++, C#, and .Net, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, Mongoid. So, that you can create an application using any of these languages. Nowadays there are so many companies that used MongoDB like Facebook, Nokia, eBay, Adobe, Google, etc. to store their large amount of data.

What is MongoDB Aggregation Framework?

Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single-purpose aggregation methods.

What is Map Reduce Function?

Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results.

Today we are gonna perform MongoDB map-reduce program and aggregation framework example

firstly we have to import the data as shown below :

after importing the data use db command to see whether the database is created or not,

mydatabase

as you can see the database is created.

Now, we have to create an aggregation function :

Aggregation function

Here,

mapper=Collection Name

aggregate()=MongoDB pre-created function

$match= it use the pre-define keyword to match the patter

.pretty()= to see the output in pretty format.

Creating Mapper function

For creating a mapper program we have to create a mapper function as shown below :

in a very simple way mapper means to filter some data.

Here,

$split: use to split the data.

Creating Reducer function

For creating a reducer program we have to create a reducer function as shown below :

Here,

.length: to find the length

Running Mapper-Reducer program

In the above image,

MapReduce(): it is a pre-created function only we have to mapper and reducer function

out: means in which collection you want to output.

as you can see myOutput1 collection is created and output is stored in that collection.

According to my data set, it finds the result according to my need where mapper program splits the data on some criteria and with reducer program length is counted.

I hope you like this blog, For next blog topic suggestions or queries you can DM me on LinkedIn. If you like this blog make sure you clap it.

Thank You

Signing off 🙌

--

--