Skip to content

MongoDB paging

Paging a large collection in memory loads every document. MongoCollection.queryPage pushes the count, sort, skip, and limit into MongoDB and returns an Arc page with the real total.

@query(service(tasks), queryOptions())
static async page(items: MongoCollection<TaskRecord>, options: QueryOptions) {
return items.queryPage({}, options);
}

GET /api/.../page?page=0&pageSize=10&sortBy=title answers with ten documents sorted by title and paging.totalItems counted by MongoDB.

  • queryPage(filter, options) defaults to the first maxPageSize rows when paging is absent. Each read is capped at maxPageSize, 100 by default and at most 10,000. If the count exceeds the cap, Arc rejects the incomplete unpaged result with 400 rather than returning partial data.
  • Only fields declared on the model may be sorted. The Arc wire name resolves to the declared property, then to its BSON name under the selected naming policy. An unknown field, including $where, answers 400.
  • An _id tie-breaker makes page order stable. An application-provided default sort is used when the request asks for none.
  • For an intentionally unbounded list, use items.find(); do not use it on large collections.

The count and the find are separate reads. A concurrent write can change the count between them; this is not a snapshot transaction.