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.