Q29: What is the .lean() method in Mongoose, and why does it boost read performance?
By default, Mongoose query results return heavy Mongoose Document instances complete with internal state, getters/setters, change tracking, and method bindings. Chaining .lean() tells Mongoose to skip document hydration and return plain JavaScript objects (POJOs), making reads up to 5x faster.
Key Takeaways
- Returns plain JavaScript objects instead of heavy Mongoose Document instances.
- Drastically reduces CPU and memory overhead for read-heavy APIs.
Knowledge Check
What capability do you lose when executing a query with .lean() in Mongoose?