Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Indexes and Performance

Lean Queries, select and Pagination

Make Mongoose queries faster - lean() for plain objects, select() projections, skip and limit versus cursor pagination, countDocuments versus estimatedDocumentCount, with runnable stand-ins.

After indexes, the biggest wins in Mongoose come from asking for less: fewer documents, fewer fields, and lighter objects. This lesson covers the tools: lean(), select(), limit()/skip(), cursor-based pagination and cheap counting.

What a Mongoose document costs

When you run User.find(), Mongoose does more than hand you the BSON data. For each result it builds a full document: it hydrates the data through the schema, attaches getters and setters, change tracking (so save() knows what changed), virtuals and instance methods. That convenience costs memory and CPU, often 3-5 times what a plain object needs.

If you only want to read data and send it as JSON, skip all of that with lean():

JavaScript
const users = await User.find({ active: true }).lean();
// users is an array of plain JavaScript objects

A lean result has no save(), no virtuals, no getters, no .id, no change tracking, and defaults are not applied to missing fields. Dates and ObjectIds are still real Date and ObjectId instances. Use lean for read-only endpoints, lists and reports; use normal documents when you will modify and save().

Node.js Document wrapper versus plain object (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

(Timings vary; the point is that the wrapper has work to do per field.)

select(): fetch only needed fields

Pulling a 50 KB body into a list of titles wastes network and memory. Project the fields:

JavaScript
await Post.find().select('title slug createdAt');   // include
await User.find().select('-passwordHash -__v');     // exclude
await Post.find().select({ title: 1, author: 1 });  // object form

Do not mix inclusion and exclusion in one projection, except that _id can be excluded from an inclusion list ('title -_id'). A schema path declared with select: false (for example passwordHash) is excluded by default; opt back in with .select('+passwordHash') when logging in. lean() and select() together give the fastest read path, and a projection that is fully inside an index can be a covered query (see Compound, text and TTL indexes).

Node.js Projection shrinks the payload (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

limit(), skip() and their limits

Never return unbounded lists. Add limit() everywhere a collection can grow. Classic page-number pagination uses skip and limit:

JavaScript
const page = 3, perPage = 20;
const posts = await Post.find({ published: true })
  .sort({ createdAt: -1, _id: -1 })
  .skip((page - 1) * perPage)
  .limit(perPage)
  .lean();

Always sort on a unique, stable key (add _id as a tiebreaker). Without it, documents with equal values can appear on two pages or none. The weakness is skip: MongoDB still walks past all skipped index entries, so page 10,000 is slow. Fine for 50 pages; a problem for deep lists.

Cursor pagination (keyset)

Instead of "skip N", say "give me the 20 items after this one". The client keeps the last item's sort key (often its _id, which is roughly time ordered):

JavaScript
const query = lastId ? { _id: { $lt: lastId } } : {};
const posts = await Post.find(query).sort({ _id: -1 }).limit(21).lean();
const hasMore = posts.length === 21;
const items = hasMore ? posts.slice(0, 20) : posts;
const nextCursor = hasMore ? items.at(-1)._id : null;

Asking for limit + 1 lets you know whether another page exists without a count query. Cost stays flat on any page, and new inserts do not shift pages. You lose "jump to page N", which infinite scroll does not need.

Node.js skip vs keyset: documents walked (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Counting efficiently

  • Model.countDocuments(filter): exact; runs a real count with your filter (uses an index when it can).
  • Model.estimatedDocumentCount(): reads collection metadata; instant, no filter, slightly approximate. Perfect for "about 1.2 million posts".
  • Avoid find().then(r => r.length): that loads everything.

Do you really need a total? "Page 3 of 812" costs a count on every request. Many UIs (Next/Previous, "Load more") work with the limit + 1 trick instead.

More performance habits

  • Use exists() (await User.exists({ email })) instead of loading a document to check presence; it returns just { _id } or null.
  • Use updateOne / $inc / $push for changes instead of load, modify, save(); fewer round trips and no lost updates.
  • Reuse the connection (one mongoose.connect per process) and size the pool with maxPoolSize, covered in Bulk writes and cursors.
  • Stream huge result sets with a cursor rather than loading arrays.
  • Cache hot, rarely-changing reads (Redis or in-process with a short TTL).
  • Profile before guessing: explain('executionStats') and MongoDB's slow query log tell you what is actually slow.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do you lose when you use lean()?

    lean() returns plain objects: faster and lighter, but without the Mongoose document features.
  2. 2. Which is the safest way to stop a password hash leaking from queries by default?

    select: false excludes the field unless you explicitly add .select('+passwordHash').
  3. 3. Why is skip(100000) slow?

    Skip cannot jump; the cost grows with the number skipped. Keyset pagination avoids it.
  4. 4. Which method returns a quick approximate total without a filter?

    estimatedDocumentCount reads collection metadata, so it is fast but cannot take a filter.

Summary

  • lean() skips document hydration for read-only data; select() limits fields.
  • Always limit() lists and sort on a unique, stable key.
  • skip pagination degrades on deep pages; cursor (keyset) pagination does not.
  • Fetch limit + 1 rows to detect a next page without counting.
  • Use exists, estimatedDocumentCount and atomic update operators instead of heavier alternatives.

Next: Bulk writes and cursors