{"success":true,"name":"paginate-mongodb-lists-with-ranges-not-growing-skips","description":"Building list pagination on a MongoDB collection","category":"ai-brimind","path":"ai-brimind/paginate-mongodb-lists-with-ranges-not-growing-skips/SKILL.md","file":"SKILL.md","content":"---\nname: paginate-mongodb-lists-with-ranges-not-growing-skips\ndescription: \"Building list pagination on a MongoDB collection\"\n---\n\n# Paginate MongoDB Lists With Ranges, Not Growing Skips\n\n## When to use\nProject uses MongoDB, and you are implementing paged lists, infinite scroll, or 'load more' over a query.\n\n## Example request\nskip(10000).limit(20) on page 500 → MongoDB still walks the 10,000 skipped docs; skip cost grows with page number. Infinite scroll feed → range/cursor pagination: filter _id or createdAt greater/less than the last seen value, backed by an index. Small admin table with 5 pages → skip/limit is fine there; the cost never grows enough to matter. Sorting by a non-unique field for cursors → ties break pagination; add _id as a tiebreaker in sort and filter. Each maps to 'skip = O(page), range cursor = O(1)'.\n\n## Instructions\nAct as a developer who checks how deep pagination can go before choosing how to paginate. Before implementing paging on MongoDB: (1) estimate the depth — a bounded admin list (few pages) can use skip/limit honestly; a feed, log, or search that can go deep cannot, because skip re-reads everything it skips, (2) for deep/infinite lists use range (cursor) pagination: sort by an indexed field, return the last item's value, and next page filters { field: { $lt/$gt: lastValue } } — constant cost at any depth, (3) make the sort deterministic: if the sort field can repeat (createdAt), add _id as a tiebreaker in both the sort and the range filter, (4) confirm an index backs the sort+filter, otherwise the cursor advantage disappears. If expected list size is unknown, ask — the answer decides the design. Only implement once depth and index are confirmed, and explain why page 1 and page 1000 will cost the same.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1050\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}