Page through MongoDB results in batches

DevOpsPad reads MongoDB results in batches of limit documents (100 by default, up to 500). When more documents match, Next batch runs the same query with skip increased by limit, and Previous batch goes back by the same amount. The generated query replaces the editor text, so you always see what ran.

Batches use offsets. They are predictable on data that is not changing, but concurrent writes can shift documents between batches. For deep or exact paging, filter on a range instead.

How it works

The MongoDB result footer showing Batch offset 0 and 1 document, the note Concurrent writes can change batches, and Previous batch and Next batch buttons.

  1. Run a find query. DevOpsPad asks MongoDB for limit + 1 documents.
  2. If the extra document exists, the result is marked as having more documents and the footer says More documents available.
  3. Next batch writes a new query with "skip": skip + limit into the editor and runs it.
  4. Previous batch writes "skip": max(0, skip − limit) and runs it.

The bar above the footer shows the current offset, for example Batch offset 200 · 100 documents.

The result grid also has its own pager of 50 rows. That pager only moves within the batch already fetched; it does not query MongoDB.

The _id tie-breaker

Offset paging is only stable if the sort order is total. If many documents share "status": "paid" and you sort on status alone, MongoDB may return ties in a different order on each query. DevOpsPad appends "_id": 1 to any sort that does not already include _id, and keeps your field order:

{ "sort": { "status": 1, "_id": 1 } }

When to use a range filter instead

skip is limited to 1,000,000, and large skips are slow because MongoDB still walks the skipped documents. When data changes during paging, offsets can also repeat or miss documents. Use the last value you saw as the start of the next query:

{
  "collection": "events",
  "filter": { "_id": { "$gt": { "$oid": "6730c1f0e4b0a1b2c3d4e5f6" } } },
  "sort": { "_id": 1 },
  "limit": 500
}

This reads the next 500 events after a known _id, no matter how deep you are. The same pattern works with a timestamp plus _id.

Limits

  • One batch holds at most 500 documents and 8 MB of Extended JSON.
  • Editing the query disables batch navigation until you run it again (Query changed. Run again to browse batches.).
  • Batches are not snapshots. MongoDB does not hold a cursor open between batches.

Frequently asked questions

Why are Previous batch and Next batch disabled?

The buttons only work for the query that produced the current result. If you edited the query, run it again first. Next batch is also disabled when there are no more documents.