Hi there,
I’m having a performance test between PostgreSQL and MongoDB.
Using a collection “employee” with 10 000 000 documents.
{
_id: ObjectId("650305a48a9b68934923a0f1"),
ename: 'Employee4327067',
age: 57,
salary: 18431,
department: 'Department23'
}
I use $group stage to group by department
{
$group: {
_id: "$department",
salary: {
$sum: "$salary",
},
},
}
0 index keys examined while I created an index department/salary and the query is to slow

Do you have an idea ?
Regards
If you have an index on that, then sort before you group by that field
With $sort stage, the query uses the index but it is not really faster
Slower than Postgres 

Without the $sort stage, the execution time is nearly the same

Do you think a search index could improve the query ?
I tested it locally with a collection of about 10M on a workstation with a slow SSD and the same index, took about 10s.
How fast does PostgreSQL do the same operation on your hardware? I assume it’s the same box running both database servers?
Hi @Emmanuel_Bernard1
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.
2 Likes
I used Mongo Compass to connect to a M20 cluster.
It took about 13s.
Does the execution time give a real information or execution statistics of the query plans ?
Do I need to write some code to execute the query and return the result ?

Hi @Jennysson_Junior,
I’m agree with you 
I’m trying to explain to a colleague that he can’t use the same modeling approach for NoSQL and SQL.
Your solution “create another collection with data modeled based on what you need and solve this query” is the good one.
I’m sure MongoDB can’t be faster then PostgreSQL if we use the same “relational” model
Thanks a lot