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
€43.50
List Price:  €60.90
You save:  €17.40
£37.39
List Price:  £52.35
You save:  £14.96
CA$69.17
List Price:  CA$96.85
You save:  CA$27.67
A$78.07
List Price:  A$109.31
You save:  A$31.23
S$65.36
List Price:  S$91.50
You save:  S$26.14
HK$387.81
List Price:  HK$542.97
You save:  HK$155.15
CHF 40.55
List Price:  CHF 56.77
You save:  CHF 16.22
NOK kr517.27
List Price:  NOK kr724.23
You save:  NOK kr206.95
DKK kr324.78
List Price:  DKK kr454.72
You save:  DKK kr129.94
NZ$83.24
List Price:  NZ$116.55
You save:  NZ$33.30
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳6,070.98
List Price:  ৳8,499.86
You save:  ৳2,428.88
₹4,258.23
List Price:  ₹5,961.86
You save:  ₹1,703.63
RM219.45
List Price:  RM307.25
You save:  RM87.80
₦80,219.95
List Price:  ₦112,314.35
You save:  ₦32,094.40
₨14,022.19
List Price:  ₨19,632.19
You save:  ₨5,610
฿1,660.65
List Price:  ฿2,325.05
You save:  ฿664.39
₺1,912.68
List Price:  ₺2,677.91
You save:  ₺765.22
B$290.37
List Price:  B$406.55
You save:  B$116.17
R931.74
List Price:  R1,304.52
You save:  R372.77
Лв85.01
List Price:  Лв119.03
You save:  Лв34.01
₩71,210.16
List Price:  ₩99,699.92
You save:  ₩28,489.76
₪185.77
List Price:  ₪260.09
You save:  ₪74.32
₱2,833.15
List Price:  ₱3,966.64
You save:  ₱1,133.48
¥7,021.45
List Price:  ¥9,830.60
You save:  ¥2,809.14
MX$981.93
List Price:  MX$1,374.78
You save:  MX$392.85
QR182.01
List Price:  QR254.83
You save:  QR72.82
P688.61
List Price:  P964.11
You save:  P275.50
KSh6,483.70
List Price:  KSh9,077.70
You save:  KSh2,594
E£2,552.66
List Price:  E£3,573.93
You save:  E£1,021.27
ብር6,546.19
List Price:  ብር9,165.19
You save:  ብር2,619
Kz45,865.82
List Price:  Kz64,215.82
You save:  Kz18,350
CLP$48,067.30
List Price:  CLP$67,298.07
You save:  CLP$19,230.76
CN¥364.54
List Price:  CN¥510.39
You save:  CN¥145.84
RD$2,986.90
List Price:  RD$4,181.90
You save:  RD$1,195
DA6,595.03
List Price:  DA9,233.57
You save:  DA2,638.54
FJ$112.49
List Price:  FJ$157.49
You save:  FJ$45
Q385.02
List Price:  Q539.06
You save:  Q154.03
GY$10,484.65
List Price:  GY$14,679.35
You save:  GY$4,194.70
ISK kr6,303.23
List Price:  ISK kr8,825.03
You save:  ISK kr2,521.80
DH461.89
List Price:  DH646.68
You save:  DH184.79
L863.83
List Price:  L1,209.44
You save:  L345.60
ден2,674.89
List Price:  ден3,745.06
You save:  ден1,070.17
MOP$399.43
List Price:  MOP$559.23
You save:  MOP$159.80
N$938.31
List Price:  N$1,313.71
You save:  N$375.40
C$1,838.39
List Price:  C$2,573.90
You save:  C$735.50
रु6,815.87
List Price:  रु9,542.77
You save:  रु2,726.89
S/185.18
List Price:  S/259.27
You save:  S/74.09
K203.18
List Price:  K284.47
You save:  K81.29
SAR187.51
List Price:  SAR262.53
You save:  SAR75.02
ZK1,423.44
List Price:  ZK1,992.94
You save:  ZK569.49
L216.50
List Price:  L303.11
You save:  L86.61
Kč1,089.97
List Price:  Kč1,526.05
You save:  Kč436.07
Ft17,792.75
List Price:  Ft24,911.28
You save:  Ft7,118.52
SEK kr474.58
List Price:  SEK kr664.46
You save:  SEK kr189.87
ARS$54,615.02
List Price:  ARS$76,465.40
You save:  ARS$21,850.38
Bs345.82
List Price:  Bs484.18
You save:  Bs138.35
COP$212,845.24
List Price:  COP$298,000.37
You save:  COP$85,155.12
₡25,121.87
List Price:  ₡35,172.63
You save:  ₡10,050.75
L1,292.24
List Price:  L1,809.24
You save:  L516.99
₲399,820.51
List Price:  ₲559,780.71
You save:  ₲159,960.20
$U2,116.99
List Price:  $U2,963.96
You save:  $U846.96
zł186.32
List Price:  zł260.87
You save:  zł74.54
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.