Pipeline in Aggregation
39K views
Oct 26, 2024
Pipeline in Aggregation
View Video Transcript
0:00
We are discussing another very important topic that is pipeline in aggregation
0:05
Now what is the pipeline? We know that pipeline means a command will take some inputs
0:10
It will produce some output. That output will be the input for the next command
0:15
It will produce output. And that output might be the input for the next commands
0:20
And this process can be repeated for multiple number of times. And that is known as a pipeline
0:26
That means input process, output. this output is the input for the next process, output will be produced and that might be carried
0:33
out for another certain levels. And that is known as the pipeline and pipeline in aggregation is a topic in this video
0:42
So what is aggregation pipelining? So pipeline means that possibility to execute an operation on some input and use the output
0:53
as the input for the next command and so on. So, as I have mentioned here
0:58
So, pipeline means the possibility to execute an operation on some set of inputs and use
1:05
the output as the input for the next command and the process can be repeated so and so on
1:12
MongoDB supports the pipeline concept in aggregation framework that there is a set of possible
1:18
stages and each of those is taken as a set of documents as an input and produces a resulting
1:25
set of documents for the final resulting JSON document at the end of the pipeline
1:31
So, some set of documents will be taken for one processing and then the output documents
1:38
will be made available for the next processing. Ultimately, the final JSON document will be obtained at the end of the pipeline
1:47
Then this can then in turn be used for the next stage and so on
1:52
So, in this way, the process will be repeated. So, let us go for some more detailing
1:58
So some pipelining methods. So these are some stages of the aggregation pipeline
2:04
So what are the stages are there? We're having dollar project. Show one specified field
2:10
You know that projection means we're doing columnar selection. So that is one specified field you are going to get
2:17
Next one is the dollar match. Match fields with some criteria. Then dollar group grouping the fields to perform aggregation
2:25
We can do some grouping on a certain field and we can do some aggregation That means for multiple values it will be reducing to a single result and then dollar short performing the shorting and dollar limit the limit
2:39
the documents to display. So, let us go for one practical demonstration for the better understanding
2:45
of this concept that is pipeline in aggregation. So here is the demonstration for you
2:51
In this video, we are going to demonstrate MongoDB pipeline aggregation. So we'll be working on the database MongoDB database that is a mine database which is having one collection that is our employee
3:04
So going for that so pseudo service MongoDB start then you shall go for this Mongo so Mongo shell has got obtained
3:16
So show DBS we are having this my database so use my database so we're going for this DB database. So we're going for this DB
3:27
yes my database is there so show collections we're having this employee so we shall go
3:35
for db dot employee dot find so this is the current content so let us
3:46
execute some queries on it to give you the demonstration on MongoDB pipeline
3:51
aggregation so find distinct city name from employee collection and then short the sequence in the descending order so at first we are trying to find out
4:01
the distinct city name and then they are to be shorted in the descending order
4:06
so we shall go for db dot employee and then aggregate then you shall go for this
4:16
dollar group so dollar city and then one curlebrace another
4:25
curleblase is this one comma we're going for this so going for short so output of
4:35
that command will be the input for the next one and that is a concept of pipelining
4:40
so id colon minus one means in the descending order so you shall go for two curleblase
4:47
complete one third bracket complete and the first bracket that is the aggregate function
4:53
is getting completed here so you can find here here we are having this Mumbai London Kolkata and Delhi and they have got arranged in the descending order there is an alphabetical order but in the descending order so next another query we going to execute so show only
5:11
the name and city of those employee whose salary is greater than or equal to
5:16
25,000 so show only the name and city of those employees whose salary is
5:24
greater than or equal to 25,000 so we shall we shall just keep
5:29
the previous one so that is our dollar match so the dollar match so I shall go
5:37
for this one as salary so this is the salary colon I shall go for another one
5:48
so is the Carlibrillus dollar GT Gt stands for greater than then in our
5:56
query it was 25,000 so I've written this that one so two Calibruses are getting completed then another
6:04
Calibrillus is getting completed so up to match it has got completed after
6:08
dollar match now I shall go for this comma and then I shall go for this dollar
6:13
project because I require only the name and city so dollar project colon
6:20
so again Carlibres underscore ID I'm giving this ID as zero and then
6:29
name going for this name then I shall go for this one in the ascending order so
6:40
then I shall go for this city and colon one because that will be also in the
6:45
ascending order so two quality braces are to be completed then one square
6:50
bracket and the completion of the aggregate method so db. employee dot aggregate then dollar match colon salary colon dollar greater than
7:01
25,000 and then comma dollar project colon underscore ID colon zero common name colon one that means name will be
7:11
add in the ascending order also the city will be arrains in the ascending order and
7:15
there is the end of the query so if I execute this one we are finding that so
7:20
these two employees are there there is the Onupam and Vivek so there
7:25
earning salary more than greater than 25,000 so you can find that 23,000 25
7:30
so this is the 32 so we are finding this onupam and the respective respective cities Delhi then 25 16 27 and that is for the Vivek and the city is
7:42
Mumbai so you are getting this respective outputs accordingly is a these are
7:47
respective output we are having okay now let us go for the another one short
7:52
records on the salary show name and salary and get only three of them so
7:59
we are we are going for this and there is a db dot employee dot aggregate so we shall go for this so here we will be using short
8:11
so colon there is our salary salary colon one and then two curly braces will get
8:22
completed then another curly breasts so here we'll be going for dollar project
8:28
so shorting and then we are going for this project and then I shall go for this
8:35
underscore ID colon zero then you shall go for this name name will be in the
8:43
ascending order then salary in the ascending order then two curly bases are
8:49
getting completed so there is a project part has got completed projection and then you shall go for this so we shall go for another one
8:58
so dollar limit column 3 so dollar limit dollar limit column 3 so that is the closing of this
9:07
Calibrous and then square bracket and closing up the aggregate function so aggregate
9:15
method so there is a salary colon one and then dollar project let me execute this one yes it is working fine
9:22
so you see if we don't provide the last one if you don't provide the last one if you don't provide
9:28
the last one if you keep up to this you are finding the all of them but when you are
9:33
giving this limit dollar limit colon 3 you are getting the only the first three of
9:38
them so what is the what was the query short records on the salary you can find
9:43
that the records the salaries have got errands in the in the ascending order so 16
9:48
thousand 18,000 203,000 25,000 25,000 27,000 and 32,000 but when you are
9:55
giving this dollar limit column 3 only the first three records are coming so in this way we have shown you that how pipeline
10:03
aggregation can be done in MongoDB thanks for watching this video
#Programming