Question 48 / 50
Good to KnowMediumConceptMongoose
What does lean() do in Mongoose, and when would you use it?
Think about it first.
Short answer
lean() returns plain JavaScript objects instead of Mongoose documents. You lose getters, setters, save(), and change tracking, and you gain CPU and memory on read-heavy paths.
Why?
List endpoints, aggregations you hydrate yourself, and anything you will not call .save() on are good lean() candidates. If you need virtuals or middleware on the way out, stay with documents. lean() is not a substitute for projection — you can and should combine both.
Interview tip
Say 'plain objects, no document overhead' and give one example: a GET list.
Common mistake
Calling lean() and then document.save(), or thinking lean() is a MongoDB server feature. It is a driver/ODM choice.
How did you do?
Cheat Sheet