Facebook Pixel

Why is skip slow for large offsets?

MongoDB still scans the skipped documents before returning the ones you want. For offset 100000, it scans 100000 docs. Use cursor-based pagination instead: find documents where createdAt is less than the last cursor. MongoDB uses an index.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Common Pagination Mistakes in Node.js (and How to Fix Them)

No pagination, no limit cap, skip for large offsets, no index on the sort field, unstable sort, sequential find and count, returning null for empty results, not returning metadata, pagination in the body, and not handling invalid cursors.

If you sort by createdAt, ties are possible. Without a tiebreaker, cursor pagination can skip or duplicate items. Use .sort({ createdAt: -1, _id: 1 }) to make the sort stable.

To avoid waterfall delay. If you await find, then await countDocuments, the total query waits for the find to finish. Promise.all([find, countDocuments]) runs both at once. Faster.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0