How to Use Mongoose Lean

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

Mongoose provides many methods for retrieving documents from a Mongoose collection. These methods include find(), findOne(), findById(), etc. The find() method is the most popular and widely used. It returns multiple documents based on the condition specified. But when there is a huge amount of documents in a collection, the find() method lacks performance. While working with a collection with less amount of documents, the performance is not affected much. But with a large number of documents, there is always a significant impact on the performance. To enhance the performance of the find() method, mongoose provides the lean() method. The lean method can be attached to the find() method for better performance. It takes less time to retrieve documents than the find() method. But the mongoose lean() method has its pros and cons. Let’s discuss how to use the lean() method in mongoose, and what are its pros and cons.

Mongoose lean() method

It is very to use the lean method in mongoose. We just need to attach it after the find() method. Have a look at the following find() method.

1
2
3
4
5
6
7
details.find({}, function(err, result) {
  if (err) {
    res.send(err);
  } else {
    res.send(result);
  }
});

This find method will return all the documents of the details collection. Now, to use the lean() method, we have to attach it after the find() method. Let’s do it.

1
2
3
4
5
6
7
8
9
details
  .find({}, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.send(result);
    }
  })
  .lean();

This is it! We just have to attach the lean() method after the find() method. Now, if we execute this method, it will do exactly what the find() method is supposed to do, i.e. retrieving all the documents. But there is one difference. With the lean() method, the average execution time is less.

So in a test, We found out, in retrieving 20 documents from a collection, the average time taken by the find() method is 218ms. But with a lean() method attached, the average time taken is 71ms. This is a huge difference. The find() method is much faster when the lean() method is attached to it. But why? The answer is that the documents returned with the lean() method are plain javascript. A simple find() query returns mongoose documents that have mongoose magic attached with them, such as getters and setters, and save function, and other things.

Conclusion

The lean () method is faster. It returns only plain objects, not mongoose documents like the find() method. There is the benefit of using the lean() method when we need less average execution time, but it does not have that mongoose magic. For these facilities we need to use the find() method without the lean() method.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.