MongoDB Aggregation Framework and Map Reduce

Vikash kaushik
4 min readSep 22, 2021

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

then here will come to know about this.

What is 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 the 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 show below

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

as you can see both the database and collections is created.

Now, we have to create a aggregation function

Here,

data=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 mapper program we have to create 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 reducer program we have to create reducer function as shown below

Here,

.length: to find the length

Running Mapper-Reducer program

Here,

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 myOutput 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.

https://github.com/vikash232/map-reducer.git

Thankyou for reading this I hope you like and if you have and queries or have any suggestions please do let me know in comment section. 🙂

--

--