Mongoose: Aggregation Vs Find very noticable performance difference

Created on 10 May 2018  路  1Comment  路  Source: Automattic/mongoose

Hi For our project we had a need to dynamically building mongoose queries .

I tried out Both aggrgation framework and I am already using find.

In the aggregation ,we are not actuallly group by anything right now.

I saw a very remarkable difference in response time with very few records ( close to 10).

aggregation takes close to 3 seconds to response back. Is this a known issue ?

Is there a guideline when we should use aggregation vs simple find query ?

Thanks,
VJ

needs clarification

Most helpful comment

@vjoshihumancare there are some unknown conditions that make this a difficult question to answer completely.

1) Are you running these tests against a mongodb server that hosted locally or on another server/cloud service?
2) Does your collection have indexes created for the path(s) you are querying?

I saw a very remarkable difference in response time with very few records ( close to 10).
3) does your collection contain 10 records? or are you returning 10 records from a larger collection?
Is there a guideline when we should use aggregation vs simple find query ?
4) If all you need to do is grab a set of documents, or specific paths from documents, find is the way to go. Aggregation provides a set of operations ( stages ) which allow you to transform the data before it's returned from the server. The decision of which to use ultimately boils down to whether or not you need/want to reshape the data before returning it from the db or just returning what exists in the db as is.

5) Have you made any attempts at using node's builtin performance hooks? You can find the docs here. This will help you narrow down exactly where the time flies.

Feel free to create a minimally complete reproducible example that demonstrates a find query and an aggregate query that demonstrates the behavior you are seeing. It will be easier to help with a code sample we can work from.

>All comments

@vjoshihumancare there are some unknown conditions that make this a difficult question to answer completely.

1) Are you running these tests against a mongodb server that hosted locally or on another server/cloud service?
2) Does your collection have indexes created for the path(s) you are querying?

I saw a very remarkable difference in response time with very few records ( close to 10).
3) does your collection contain 10 records? or are you returning 10 records from a larger collection?
Is there a guideline when we should use aggregation vs simple find query ?
4) If all you need to do is grab a set of documents, or specific paths from documents, find is the way to go. Aggregation provides a set of operations ( stages ) which allow you to transform the data before it's returned from the server. The decision of which to use ultimately boils down to whether or not you need/want to reshape the data before returning it from the db or just returning what exists in the db as is.

5) Have you made any attempts at using node's builtin performance hooks? You can find the docs here. This will help you narrow down exactly where the time flies.

Feel free to create a minimally complete reproducible example that demonstrates a find query and an aggregate query that demonstrates the behavior you are seeing. It will be easier to help with a code sample we can work from.

Was this page helpful?
0 / 5 - 0 ratings