MongoDB {aggregation $match} vs {find} speed
Solution 1:
The main purpose of the aggregation framework
is to ease the query of a big number of entries and generate a low number of results that hold value to you.
As you have said, you can also use multiple find
queries, but remember that you can not create new fields with find
queries. On the other hand, the $group
stage allows you to define your new fields.
If you would like to achieve the functionality of the aggregation framework
, you would most likely have to run an initial find
(or chain several ones), pull that information and further manipulate it with a programming language.
The aggregation pipeline
might seem to take longer, but at least you know you only have to take into account the performance of one system - MongoDB engine.
Whereas, when it comes to manipulating the data returned from a find
query, you would most likely have to further manipulate the data with a programming language, thus increasing the complexity depending on the intricacies of the programming language of choice.
Solution 2:
Have you tried using explain() to your find queries? It'll give you good idea about how much time find() query will exactly take. You can do the same for $match with $explain & see whether there is any difference in index accessing & other parameters.
Also the $group part of aggregation framework doesn't utilize the indexing so it has to process all the records returned by $match stage of aggregation framework. So to better understand the the working of your query see the result set it returns & whether it fits into memory to be processed by MongoDB.