useMongocampFetchAll
Reads every page of a document or file list — with a cap, progress and cancellation. It follows the server's cursor (after= / x-pagination-next), so every page costs the same and documents written during the read don't shift the others.
Not used by the module itself
Bulk delete/edit by filter, copy, export and import now run on the server, so none of the module's own components call fetchAllDocuments / fetchAllFiles any more. They stay a public helper for apps that need every row of a collection in the browser (the document search is POSTed, so long filters work).
const { fetchAllDocuments, fetchAllFiles } = useMongocampFetchAll()
const controller = new AbortController()
const { rows, total, truncated } = await fetchAllDocuments('customers', {
filter: 'country:"DE"',
filterCaseInsensitive: false, // true = match the filter's wildcards without regard to case
sort: ['name'],
projection: ['_id'], // the cheap way to collect ids
pageSize: 500, // default 500
maxRows: 50_000, // default 10 000
signal: controller.signal,
onProgress: ({ loaded, target }) => (progress.value = loaded / (target ?? loaded)),
})fetchAllFiles(bucketName, { filter, sort, projection, … }) does the same for the files of a GridFS bucket (there sort and projection are strings, as the file list takes them, and filter is a MongoDB query as JSON text, not Lucene — the file list ignores Lucene).
Options and result
| Option | Meaning |
|---|---|
pageSize | rows per request, default 500 |
maxRows | stop after this many rows, default 10 000 — raise it on purpose, an unbounded read is never the default |
onProgress | called after every page with { loaded, total, target, page }; target is the total capped at maxRows; synchronous (not awaited) |
signal | an AbortSignal — see below |
keyOf | identity of a row (row => row._id), to drop a row that shows up on two pages |
Returns { rows, total, truncated, totalChanged, duplicates, pages }: total is the server's count from the first page (later pages are requested with count=none), truncated says more rows exist than were read (the cap was hit), duplicates how many rows keyOf dropped. totalChanged is always false for a cursor read (it is kept for the page-number reader fetchAllPages).
What it relies on
- The first request sends an empty
after=; every answer carriesx-pagination-nextwhile more pages exist, and the read stops when it is missing, when a cursor repeats, on an empty page or atmaxRows. No page number is ever sent (the server refusesaftertogether withpage). - The server honours
rowsPerPagefar beyond its default of 100, so the page size is the speed lever, not concurrency (the pages are read one after the other). Very large documents make a big page heavy: lowerpageSizefor them. - A cursor orders by the
sortand then_id, so the order is total even when the sort has ties.
Cancelling
Aborting makes the call reject with an AbortError (or the signal's own reason), also while a request is in flight — a half-read list is never returned as if it were complete, so a caller cannot quietly end up with a partial list. Every page also has its own 60 s timeout.
Documents changing while it reads
A document added or removed during the read does not shift the others, so no unchanged document is missed or repeated (checked live: 25 000 documents read while 279 were inserted — none lost or doubled).
Rows
Document rows are relaxed Extended JSON, as the server sends them: _id is { $oid }, dates { $date }, integers beyond 2^53 { $numberLong }, decimals { $numberDecimal } and date-looking text { $string }; int and double are plain numbers. Use idOf for an id as text and unwrapExtendedJson for plain values (see useMongocampExtendedJson). Breaking: these rows used to be plain (ids and dates as strings). File rows stay plain (FileInformation). Calls go through useMongocampClientApi, so session and server proxy mode both work. The pure functions are fetchAllByCursor(fetchPage, options) and, for lists without a cursor, fetchAllPages(fetchPage, options) in utils/pagedFetch.ts.
Related
useMongocampBulkDelete— deleting a set of ids in chunksuseMongocampExport— turning rows into CSV