When to Use Mongoose’s .lean(): Boost Read Performance by Skipping Hydration
Use Mongoose’s .lean() to skip document hydration on read‑heavy endpoints, saving CPU and memory. Learn when it’s safe, how to enable virtuals, and benchmark the benefit.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Use Mongoose’s .lean() to skip document hydration on read‑heavy endpoints, saving CPU and memory. Learn when it’s safe, how to enable virtuals, and benchmark the benefit.
On a read-heavy Mongoose list endpoint (assume Mongoose 7/8, Node 18+, MongoDB 6), the plan is to measure before optimizing. The candidate lever is the documented query.lean() option, which returns plain JavaScript objects instead of hydrated documents, skipping change tracking, getters, and middleware state. But that only helps if request time is dominated
Goal I want to reduce heap usage when reading large collections in a Node.js service by using Mongoose's lean() query option, while still resolving referenced documents through populate() on a few fields. Constraints and uncertainty The documentation states that lean() returns plain JavaScript objects instead of hydrated Mongoose documents, which removes per