Performance plays a big part in any application and one of the most vital parts in any application’s performance is it’s queries. Therefore, it becomes absolutely vital to write better mongoose queries if you are creating a NodeJS application.
I work with a team of great developers and have learned a lot of tips and tricks for writing better queries overtime. In this article I will share some of my favourite best practices to write better mongoose queries that will improve the performance of your node application.
1. Use Projection
It is really important to query for only the data fields that you need , i.e, we should not bring the data from the database that will not be used by us. Mongoose provides us with a select
method that will only bring certain fields with the documents when we query over the collection.

In the above query, we query over the User collection to find all users with age greater than 18. Now, our User document might have any number of fields and we might be interested in just displaying the list of 18+ users in the frontend with their names. Then, do we need to pull out details like user.address or user.username? Obviously not. When we only need the name and age, we should only ask for the name and age as we have !
The select chain operator will get the users with only the id, name and age fields. This will greatly improve the data being transferred and improve the performance of your application.
2. Use lean()
One thing that can greatly improve your find queries is using the lean() chain operator with your query.
What we know is that Mongoose returns an instance of the Mongoose Document class, which is quite large in size when compared to a Javascript object as it contains a lot of internal features. Now, if we want to get the returned document as just a POJO(Plain old javascript object) we can use the lean chain operator this way ->

Using lean decreases the size of the object being returned which greatly improves performance. A thing to be noted though is that it can only be used for finding and not for anything else! You cannot even keep track or change the POJO to save again. lean() should only be used in operations we do just for reading data.
3. Promise.all
One thing you must be knowing (or should know) is that database queries return a promise. So, usually you would have seen await written in front of the query so as to resolve the promise there and then.
But one of the best things you can do is to use Promise.all() to resolve your promises in parallel which would greatly improve performance of the application as it is equivalent to those queries running at the same time.
This though can only be done if the queries are not inter-dependent and do not need data that the other query brings.

Here, we can see that both the queries are independent and therefore their promises can be resolved in parallel in the Promise.all() function.
These three practices will greatly improve your app’s performance and have no significant negative points so I think you can easily use them in your day to day querying!
Comments
Post a Comment