This kind of test can cause a huge confusion about databases.
You are just putting millions of documents in a collection and doing one operation but we need to go deeper in this comparison. For you to have the benefits from any tool, there are several things that you must do, and MongoDB isn’t different.
One of the most important things using MongoDB is modeling your data based on your needs and your model of documents looks terrible to get a sum of salaries by the department.
Your query is loading all documents on memory to make the operation and it’s very bad to server do it.
So one of the things that you can solve your problem is to create another collection with data modeled based on what you need and solve this query. You will have data duplicated but for MongoDB designs, there is no problem with that.