Populate vs lean: what should I remember?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Populate vs lean: what should I remember?
Definition:
populate resolves references so related records are available in a query result. lean skips creating full Mongoose document instances and returns plain JavaScript objects. One controls related-data retrieval and the other controls result hydration, so they solve different performance concerns.
Key points:
-
populatefetches referenced records; watch the extra queries and data size. -
leanreturns plain objects without normal document methods or change tracking. -
Use
leanfor read-only results when document features are not needed. -
Lean results do not have normal document
savebehavior; features such as getters and virtuals need explicit consideration.
Example — Mongoose:
const users = await User.find({ active: true })
.select('name email')
.limit(20)
.lean();- These are plain objects for a read response; do not expect to call
users[0].save().
Interview point:
- Lean reduces application-side overhead but does not repair a poor database query or missing index.
Adapted from 04_MONGODB_MONGOOSE.md · “Populate vs lean: what should I remember?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.