How to write mapper and reducer function by using MongoDB aggregation
Hello, this article gives you, how we can write mapper and reducer functions by using MongoDB aggregation.
Before going ahead you need to ready the Mongo DB server and if you use local service then also some MongoDB tools to import the data.
What is MongoDB Aggregation-:)
I am going to tell this answer by using very example data, lets assume that why we use MongoDB,
the answer is we use MongoDB to store the data, ok now I want to tell you that right we store the data in MongoDB, So in Compony, we store only data to improve the service, do some decisions and many kinds of analytics and analysis we do.
We need to do some operation on the top of data, So in MongoDB MongoDB aggregation is a concept of framework that will help you to do operations on the top of store data in mongodb
How we do operation on the top of data by using MongoDB Aggregation -:)
I want to show you a demo of how we can do
Note-: In this demo, I am going to import the data in my local MongoDB server so I need mongoimport tools so we can use this tool to import as
mongoimport persons.json -d mydata -c data - -jsonArray
Here,
- -d -: it tells the database name
- -c -: it tells the collection name
Now let’s do some operations on top of this data to just see how it works
Note-: I imported data okay, but what to do on the top of data it decided by you, because before doing operation on the data you should know about the data or what’s your need
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
Not move toward creating Mapper and Reducer function, one thing I want to tell here that you can use the official doc for aggregation this is so simple only one thing you should know is about the data and what to do.
Creating Mapper function-:)
In very simple mapper means filter some data.
Here,
$split: use to split the data
Note-: which I wrote it is my use cases, it comes when you decide what to do
Creating Reducer function-:)
Here,
.length: to find the length
Doing Operation-:)
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.
See above my output store in a collection
According to my data set, it finds the result as I wanted,
For your knowledge in my
Mapper Program I just split the data on some criteria
Reducer Program I just counted the length
Like this, you can write the function and we can easily use it with MongoDB data or with big data.
Keep Sharing Keep Learning
Keyword-: MongoDB aggregation
MD MOQADDAS