Skip to content

Paging & Counting ​

The document list, find, search, aggregate, distinct and the file list are paged.

Page paging ​

rowsPerPage (default 100) and page (1-based) are query parameters. The answer is the list. The paging information is in headers:

HeaderContent
x-pagination-rows-per-pagerows per page
x-pagination-current-pagethe page
x-pagination-count-rowsall matching documents
x-pagination-count-pagesnumber of pages

The headers are exposed through CORS (CORS_HEADERS_EXPOSED), so browsers can read them. A browser that cannot read x-pagination-next ends a cursor read after the first page; check with curl -sD - -o /dev/null -H "Origin: <site>" <server>/version. A database that still stores an old default gets the current list at the start since 2.7.1.

Skipping the count: count=none ​

Counting a large result costs about as much as reading it. count=none skips the count and leaves out x-pagination-count-rows and x-pagination-count-pages. count=exact is the default. Without a filter the count comes from the collection metadata, which is cheap.

Cursor paging: after ​

Deep pages with page get slower, because the database skips (page - 1) × rowsPerPage documents. Cursor (keyset) paging starts each page after the last document of the previous one, so every page costs the same:

  1. Send after= (empty) for the first page.
  2. Read the x-pagination-next header of the answer.
  3. Send it as after=<token> for the next page. When the header is missing, the page was the last one.
http
GET /mongodb/collections/orders/documents?sort=-createdAt&rowsPerPage=50&after=
→ x-pagination-next: eyJzIjpbWyJjcmVhdGVkQXQiLC0xXSxbIl9pZCIsMV1dLC...

GET /mongodb/collections/orders/documents?sort=-createdAt&rowsPerPage=50&after=eyJzIjpbWyJjcmVhdGVkQXQiLC0xXSxbIl9pZCIsMV1dLC...
  • page and after cannot be combined (400).
  • The sort always ends with _id, so the order is total and every document comes exactly once.
  • The token is opaque. Keep the same filter and sort for all pages.
  • A sort field must hold one BSON type in all documents (or null/missing). An array in a sort field does not page correctly.

Cursor paging is available on the document and file lists.

Released under the MIT License.