Habilelabs-Logo
Blog

Lean function for Mongoose query performance

April 13th, 2017 . 2 minutes read
Blog featured image

As we know that performance is a major issue while working with big data in modern web applications development. So follow this small trick to achieve performance in mongoose query. so let’s learn how to enhance mongoose query performance.

Mongoose Lean chain function that returns a simple JavaScript object, By default mongoose, Find query returns mongoose object with over 1000+ functions with each document.

First, let me explain a very common issue that all we face.

If we want to change the result in mongoose object.

Possible solutions can be :

1. With the help of ‘toObject()’ function,  but it doesn’t provide performance. It just converts mongoose document to javascript object.

Example :

2. Use mongoose Lean chain function

As I mentioned above, the lean chain function provides JavaScript object so the performance of find and find one query execution time will be just twice.

How can I use Mongoose lean chain function?

When shouldn’t you use this chain function?

If you want to update result document like

user.save()

You shouldn’t use this in this case otherwise save() function will not work.
This small trick will make your query execution fast.

Conclusion

The high performance of MongoDB technology is a big issue while working with big data in modern applications. I hope mongoose lean function article is helpful for performance enhancement.

If you are looking for more tricks for performance read our article about indexing and Query Optimizer that also improves the performance of mongoose query. If you have any problem or query then ask in the comment box, now!

Share with your friends now, if you enjoy this post.

Author: surendra
Share: