Another great tool for grouping MongoDB documents is a function called MapReduce. At first, this may look really complicated, but this has got more power than the regular aggregate function. So let's go check this out. The MapReduce function takes a couple of parameters First, let's say DB dot employees dot MapReduce. The first argument is a function typically named as map. The second parameter is a function generally known as reduce.
The third one is a bunch of options that we can supply. Now at this time, all these are considered as variables and I don't have these variables. Let's create one by one, function, map. Function reduce, and war options, which is an empty object at this time. The map function will be executed for every document in the employees collection and we have access to that In the form of this, the map function generally calls another function called Emmet, which sends a key to be used, for example, this dot department. And then a value that we want to summarize.
For example, this start salary. The outcome of the map function is a key value based collection, where we will have unique department names as keys. And for every department, we will have an array of salaries. Now that collection is supplied to the reduce function with a key and values. So if this map function was called for every document, and we have let's say for example, n number of unique departments, the reduced function will be called exactly n number of times where a key which is the department and an array of all the salaries is supplied. So what we can do here is to return an object, which is a summary of this particular values.
For example, I can simply return array dot sum of values, which means we are summing up all the salaries for a given department and returning that. So the outcome of this reduce function is that MongoDB takes the key and summarized salary, and then we'll add it to its bucket. At the end of this, the entire bucket, which is a key, and the sum of salaries will be given to us. This options tells us how to handle the response data. And at this time, we can simply say out is inline. Save it and then we can just access this as Mongo, e x 13 dot j s. And we have a summary report.
So if you carefully observe, each department is displayed one time and this happens to be the sum of all the salaries In the beginning, I mentioned that MapReduce offers more power than the typical aggregate function. And that's because we can control whether or not a particular piece of data is emitted based on certain condition. For example, if I don't want to include the salaries less than or equals to 4000, I can do so by simply adding a condition if this dot salary is less than or equals to 4000 return which means out of n number of documents in the employees collection, certain of them are ignored based on this condition. So now if I save and access the result, once more, you will see that the numbers are different and smaller compared to the previous one. Also, we can add less number of departments to include for example, let's say I don't want to include sales services support and training.
I can add one more condition Save it, execute the command once more. And you will see that in the output we do not include sales support services and training departments.