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
€46.41
List Price:  €64.99
You save:  €18.57
£39.85
List Price:  £55.80
You save:  £15.94
CA$68.38
List Price:  CA$95.74
You save:  CA$27.36
A$75.69
List Price:  A$105.97
You save:  A$30.28
S$67.65
List Price:  S$94.71
You save:  S$27.06
HK$390.96
List Price:  HK$547.38
You save:  HK$156.41
CHF 45.35
List Price:  CHF 63.50
You save:  CHF 18.14
NOK kr542.95
List Price:  NOK kr760.18
You save:  NOK kr217.22
DKK kr346.22
List Price:  DKK kr484.74
You save:  DKK kr138.51
NZ$83.20
List Price:  NZ$116.49
You save:  NZ$33.28
د.إ183.60
List Price:  د.إ257.06
You save:  د.إ73.45
৳5,484.79
List Price:  ৳7,679.15
You save:  ৳2,194.35
₹4,174.22
List Price:  ₹5,844.24
You save:  ₹1,670.02
RM236.85
List Price:  RM331.61
You save:  RM94.76
₦61,737.65
List Price:  ₦86,437.65
You save:  ₦24,700
₨13,903.63
List Price:  ₨19,466.20
You save:  ₨5,562.56
฿1,842.69
List Price:  ฿2,579.91
You save:  ฿737.22
₺1,612.90
List Price:  ₺2,258.19
You save:  ₺645.29
B$253.75
List Price:  B$355.28
You save:  B$101.52
R922.13
List Price:  R1,291.06
You save:  R368.92
Лв90.85
List Price:  Лв127.20
You save:  Лв36.34
₩67,969.88
List Price:  ₩95,163.27
You save:  ₩27,193.39
₪185.94
List Price:  ₪260.34
You save:  ₪74.39
₱2,861.35
List Price:  ₱4,006.12
You save:  ₱1,144.77
¥7,722.34
List Price:  ¥10,811.89
You save:  ¥3,089.55
MX$843.21
List Price:  MX$1,180.56
You save:  MX$337.35
QR182.28
List Price:  QR255.22
You save:  QR72.93
P678.57
List Price:  P950.05
You save:  P271.48
KSh6,648.67
List Price:  KSh9,308.67
You save:  KSh2,660
E£2,385.52
List Price:  E£3,339.92
You save:  E£954.40
ብር2,869.28
List Price:  ብር4,017.22
You save:  ብር1,147.94
Kz41,825.96
List Price:  Kz58,559.69
You save:  Kz16,733.73
CLP$46,485.20
List Price:  CLP$65,083
You save:  CLP$18,597.80
CN¥354.33
List Price:  CN¥496.09
You save:  CN¥141.76
RD$2,899.21
List Price:  RD$4,059.13
You save:  RD$1,159.91
DA6,716.85
List Price:  DA9,404.13
You save:  DA2,687.28
FJ$112.23
List Price:  FJ$157.14
You save:  FJ$44.90
Q388.45
List Price:  Q543.86
You save:  Q155.41
GY$10,455.72
List Price:  GY$14,638.84
You save:  GY$4,183.12
ISK kr6,977.10
List Price:  ISK kr9,768.50
You save:  ISK kr2,791.40
DH500.96
List Price:  DH701.39
You save:  DH200.42
L885.56
List Price:  L1,239.86
You save:  L354.29
ден2,861.59
List Price:  ден4,006.46
You save:  ден1,144.86
MOP$402.29
List Price:  MOP$563.24
You save:  MOP$160.94
N$920.44
List Price:  N$1,288.69
You save:  N$368.25
C$1,839.55
List Price:  C$2,575.52
You save:  C$735.96
रु6,679.37
List Price:  रु9,351.66
You save:  रु2,672.28
S/186.27
List Price:  S/260.79
You save:  S/74.52
K193.32
List Price:  K270.67
You save:  K77.34
SAR187.48
List Price:  SAR262.50
You save:  SAR75.01
ZK1,358.11
List Price:  ZK1,901.47
You save:  ZK543.35
L231.01
List Price:  L323.43
You save:  L92.42
Kč1,161.35
List Price:  Kč1,625.98
You save:  Kč464.63
Ft18,056.25
List Price:  Ft25,280.20
You save:  Ft7,223.94
SEK kr542.25
List Price:  SEK kr759.20
You save:  SEK kr216.94
ARS$44,003.90
List Price:  ARS$61,608.98
You save:  ARS$17,605.08
Bs345.36
List Price:  Bs483.53
You save:  Bs138.17
COP$195,270.45
List Price:  COP$273,394.26
You save:  COP$78,123.80
₡25,568.20
List Price:  ₡35,797.53
You save:  ₡10,229.32
L1,234.61
List Price:  L1,728.55
You save:  L493.94
₲373,712.67
List Price:  ₲523,227.64
You save:  ₲149,514.97
$U1,909.55
List Price:  $U2,673.53
You save:  $U763.97
zł200.30
List Price:  zł280.44
You save:  zł80.13
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.