Aggregation

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€44.43
List Price:  €62.21
You save:  €17.77
£37.59
List Price:  £52.63
You save:  £15.03
CA$69.69
List Price:  CA$97.58
You save:  CA$27.88
A$77.99
List Price:  A$109.20
You save:  A$31.20
S$64.90
List Price:  S$90.87
You save:  S$25.96
HK$388.85
List Price:  HK$544.43
You save:  HK$155.57
CHF 41.57
List Price:  CHF 58.21
You save:  CHF 16.63
NOK kr518.43
List Price:  NOK kr725.84
You save:  NOK kr207.41
DKK kr331.53
List Price:  DKK kr464.18
You save:  DKK kr132.64
NZ$84.57
List Price:  NZ$118.40
You save:  NZ$33.83
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.46
৳6,093.85
List Price:  ৳8,531.88
You save:  ৳2,438.02
₹4,269.78
List Price:  ₹5,978.04
You save:  ₹1,708.25
RM215.03
List Price:  RM301.06
You save:  RM86.03
₦80,339.42
List Price:  ₦112,481.62
You save:  ₦32,142.20
₨14,092.14
List Price:  ₨19,730.13
You save:  ₨5,637.98
฿1,648.17
List Price:  ฿2,307.57
You save:  ฿659.39
₺1,937.88
List Price:  ₺2,713.19
You save:  ₺775.30
B$282.36
List Price:  B$395.33
You save:  B$112.97
R909.80
List Price:  R1,273.80
You save:  R363.99
Лв86.89
List Price:  Лв121.66
You save:  Лв34.76
₩69,780.43
List Price:  ₩97,698.18
You save:  ₩27,917.75
₪177.65
List Price:  ₪248.73
You save:  ₪71.07
₱2,767.79
List Price:  ₱3,875.13
You save:  ₱1,107.34
¥7,270.27
List Price:  ¥10,178.95
You save:  ¥2,908.68
MX$971.97
List Price:  MX$1,360.84
You save:  MX$388.86
QR182.01
List Price:  QR254.83
You save:  QR72.82
P680.79
List Price:  P953.17
You save:  P272.37
KSh6,466.20
List Price:  KSh9,053.20
You save:  KSh2,587
E£2,529.10
List Price:  E£3,540.94
You save:  E£1,011.84
ብር6,728.99
List Price:  ብር9,421.13
You save:  ብር2,692.13
Kz45,590.88
List Price:  Kz63,830.88
You save:  Kz18,240
CLP$46,732.39
List Price:  CLP$65,429.09
You save:  CLP$18,696.69
CN¥361.19
List Price:  CN¥505.70
You save:  CN¥144.50
RD$2,951.01
List Price:  RD$4,131.65
You save:  RD$1,180.64
DA6,652.73
List Price:  DA9,314.35
You save:  DA2,661.62
FJ$113.56
List Price:  FJ$159
You save:  FJ$45.43
Q385.11
List Price:  Q539.19
You save:  Q154.07
GY$10,461.12
List Price:  GY$14,646.40
You save:  GY$4,185.28
ISK kr6,524.97
List Price:  ISK kr9,135.48
You save:  ISK kr2,610.51
DH462.53
List Price:  DH647.58
You save:  DH185.05
L854.60
List Price:  L1,196.51
You save:  L341.91
ден2,738.05
List Price:  ден3,833.49
You save:  ден1,095.44
MOP$400.55
List Price:  MOP$560.81
You save:  MOP$160.25
N$933.50
List Price:  N$1,306.98
You save:  N$373.47
C$1,845.09
List Price:  C$2,583.28
You save:  C$738.18
रु6,780.01
List Price:  रु9,492.56
You save:  रु2,712.54
S/182.01
List Price:  S/254.83
You save:  S/72.82
K202.87
List Price:  K284.03
You save:  K81.16
SAR187.51
List Price:  SAR262.53
You save:  SAR75.01
ZK1,390.76
List Price:  ZK1,947.17
You save:  ZK556.41
L227.47
List Price:  L318.48
You save:  L91
Kč1,108.70
List Price:  Kč1,552.28
You save:  Kč443.57
Ft17,958.90
List Price:  Ft25,143.90
You save:  Ft7,185
SEK kr485.48
List Price:  SEK kr679.72
You save:  SEK kr194.23
ARS$55,571.28
List Price:  ARS$77,804.24
You save:  ARS$22,232.95
Bs345.29
List Price:  Bs483.44
You save:  Bs138.14
COP$212,466.38
List Price:  COP$297,469.93
You save:  COP$85,003.55
₡25,326.52
List Price:  ₡35,459.16
You save:  ₡10,132.63
L1,301.19
List Price:  L1,821.77
You save:  L520.58
₲401,593.41
List Price:  ₲562,262.91
You save:  ₲160,669.49
$U2,088.44
List Price:  $U2,923.98
You save:  $U835.54
zł188.05
List Price:  zł263.29
You save:  zł75.23
Already have an account? Log In

Transcript

Using grouping techniques, we can get summarized data from a collection. This can be done in a couple of different ways. Let's have a look at the aggregate function first. For this demo, I'll be using a collection called employees. Here is what it looks like. Let's say we want to find the total number of employees in a particular department.

So this is what we can do DB dot employees dot aggregate. The first parameter to this is the grouping option. We have an operator called group and then we have to specify what is the ID to be used. The ID here is a property on which we want to group since we want to find out the total number of employees working in a department. So this has to be a department and in order to specify that this is not a value, but this is one of the property, we prefix this with dollar. So hence we have to use dollar department.

This is followed by the new column or property that we want to get. So which may be an employee count, and we have to specify how this count is calculated. Now, unfortunately there is no operator called count. So what we can do is, we can repeatedly add one using the sum operator for every employee in a particular department. And this gives us the count of the employees in this particular department. So, let's save this and see if we can execute the same.

As you can see, we have a nice summary of department and the number of employees working in that department. Let's take another example. This time I want to find the average salary of every employee in a particular department to do so I can just comment this And let's write one more query DB dot employees dot aggregate. As in the previous case, the ID property would be the department and dollar department, average salary for that particular department and we can use the operator called dollar a VG on the field salary. Once again like dollar department, we are using dollar salary because if you do not give dollar salary, but just salary will assume salary is a text and not a property, save it and run the same command and we get the average salary in each department. We can also use the match operator to reduce this output from displaying all the average salaries to only where average salary is more than a particular limit or less than a percent.

Limit. For example, let's say we want to display those departments where an employee earns an average of more than $4,000. So I can use another operation here. And I will say dollar match. And what is the match criteria in our case, we wanted to say the average salary to be greater than or equal to 4000. Save it, run the same command, and this time we'll see a smaller subset where every average salary printed is more than $4,000.

And if you want to order the output in a particular ascending or descending order, we can use that as the third parameter to the aggregate function. So we'll use $1 sort and then we specify the property on which we want to start, in this case, a VG salary. And if you want ascending, you can give it as one if you want descending, you can give it as minus One. Let's save it and execute the same and here is the output.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.