Aggregation Operation Examples
40K views
Oct 24, 2024
Aggregation Operation Examples
View Video Transcript
0:00
In this video we are discussing one example on aggregation operation
0:07
So aggregation operation example. In this section we'll see how to use the different aggregate functions
0:13
So like your dollar sum, dollar mean, dollar max, etc. So I think for the better idea, let us go for one practical demonstration to show you better
0:24
that how this aggregate functions can be used or aggregation functions can be used in
0:29
our MongoDB queries. In this demonstration we are discussing about MongoDB aggregation
0:37
So at first we're opening one terminal. So control alter T we have opened one terminal
0:42
here and in this terminal we'll be doing some query execution and we'll be doing on
0:49
the MongoDB database already created that is a my database containing the collection
0:54
that is our employee. So at first we're going for pseudo service
0:59
mongo db start asking for the password then mongo so we are going for the mongo shell so mongo shell has been obtained so we shall go for show db s so you can find that we are having one database that is the my database so use my database so now going for
1:29
db my database is there show so collections so we can go for db employee dot find db employee dot find so this is the respective data we are having
1:48
in this database my database and the collection name is employee so let us
1:54
execute some queries on it so find the sum of salaries of all employees so how
1:58
to execute this one so db. employee dot aggregate then dollar group then colon
2:08
we're going for this ID so for this ID you are putting the value null then count
2:15
colon we're going for this as dollar sum because we're going for the sum of the
2:24
salaries so dollar salary so we have opened three calibrassus so we are closing them and one first bracket
2:34
so this is the total case and it should be also a carbure brace so you see db
2:41
employee dot aggregate so aggregate is a method and then we are giving this one
2:46
as dollar group colon underscore id colon null so in the output couple the ID will be
2:53
null and we are going for this count colon that is a sum
2:58
salary in this way we're going to get this one so if I press enter I'm
3:02
getting this sum of the salary as 16600 that is the sum of the salary so instead of count also we can go for total salary so we getting this one ID is null and total salary is this next one find the age of the youngest employee from the collection so here here you can see that this is the youngest employee means we are going to get this 12 here so this 12 we are expecting so
3:35
the query will be db.employee dot aggregate then dollar group is there then underscore id
3:41
colon null and then we shall go for this mean edge so there is a key here so we shall go for
3:50
dollar mean so instead of sum here will be using this mean and obviously we'll be operating
3:55
on edge so you can find that the minimum age is 12 and that we pointed out earlier
4:02
okay we're going for the next query the next query is that find the average salary from the employee collection so how to do that one so
4:12
here we'll be going for this so id is null there and then we are going for this average
4:21
so we're going for this average and then here we'll be going for this avigee and we'll be
4:29
operating on salary so in this way we're working So this is the average salary we have got
4:38
Okay. Now let us go for the another one Find the average salary of all employees grouped by different cities so if I want to execute this one so find the average salary of all employees grouped by
4:52
different cities so dollar group is there so minus colon this is my
4:59
underscore ID colon here we can put here dollar city instead of null then you shall go
5:07
for this average salary then we are going for this this one and then we shall go for this dollar salary so here you can find that
5:16
id here is dollar city so now if i go for enter you are finding this one Kolkata is having the
5:24
average salary of 17,000 so let us go for a cross check for Kolkata we are having two salaries
5:30
one is the 16 000 another one is the 18000 so the average is 17 000 is quite okay
5:36
so let us go for this delisay we're having this 28 500 because for Delhi we're having this 25,000 and for
5:44
Delhi we're having this 32,000 so difference is our 7,000 by two so three
5:51
thousand five hundred so 28,500 is just the correct one and for London we're
5:57
having only one instance of London so it is average salary salary will even the
6:02
same that is at 23,000 so in this way we have shown you that how the average
6:07
salary of all employees group by different cities can also be calculated so in this demonstration we have shown you that how this
6:14
aggregation operations can be done on MongoDB databases thanks for watching this
#Programming