useMongocampExportAll
Exports a whole collection, or every document matching a filter, as JSON, NDJSON or CSV — not just the rows on screen (that is useMongocampExport, which the table's page export uses). The server streams the file (GET …/documents/export): there is no document cap, and the browser never parses a document.
const { exportCollection } = useMongocampExportAll()
const controller = new AbortController()
const result = await exportCollection({
collection: 'customers',
format: 'csv', // 'json' | 'ndjson' | 'csv'
filter: 'country:"DE"', // optional Lucene filter
expected: 1834, // optional: the document count of a complete JSON file
signal: controller.signal,
onProgress: ({ bytes, lines }) => (received.value = bytes),
})
if (result.incomplete) toast('The server stopped early — the file is incomplete')Options
| Option | Meaning |
|---|---|
collection, format | what to export and as what |
filter, sort, projection | as for the document list; projection: ['_id', 'name'] limits the fields and, for a CSV, is its columns, in that order (columns is the old name for it). All of them travel in the request body (POST …/documents/export, server 2.2), so a filter can be of any length |
filterCaseInsensitive | true matches the filter's wildcards without regard to case (the server matches them case-sensitively otherwise); sent only with a non-empty filter |
extendedJson | JSON/NDJSON only: 'relaxed' (default) or 'canonical' — see The formats; ignored for CSV |
maxRows | the server's limit: stop after this many documents (default: all) |
expected | how many documents you expect (the count of the filter): the documents of a complete JSON file (NDJSON and CSV report the server's own count) |
onProgress, signal | onProgress({ bytes, lines }) after every chunk of the stream; aborting rejects with an AbortError and downloads nothing |
filename | default <collection>-<yyyymmdd>-<hhmm>.<format> (the name made safe for a file) |
download | false only returns the blob instead of handing it to the browser |
Returns { blob, filename, mimeType, format, bytes, documents?, incomplete }.
incomplete means the file is cut short. The server writes the stream while the database delivers it, so a failure midway ends the stream — with a normal 200. The file is therefore checked: JSON must end with ]; NDJSON and CSV are asked with trailer=true (server 2.2) and must end with the server's end marker ({"$end":true,"count":n} / # end n) — an error marker ({"$error":…} / # error …) or none at all means cut short. The marker line is removed from the file, so it re-imports cleanly. The file is still handed to the browser, so always tell the user. documents is the marker's count for NDJSON and CSV, and the expected count for a complete JSON file.
The formats
| Format | Content |
|---|---|
json | one array of documents |
ndjson | one compact document per line — what mongoimport and most tools read, and what the import wizard now reads too |
csv | a header row and one row per document; nested values as JSON, a text with a comma, a quote or a line break in quotes. The columns are the projection, by default the fields of the first 1 000 documents |
JSON and NDJSON files are relaxed Extended JSON (breaking — they used to be plain JSON): _id is { "$oid" }, dates { "$date" }, integers beyond 2^53 { "$numberLong" }, decimals { "$numberDecimal" }, date-looking text { "$string" }; int and double are bare numbers. A re-import keeps ids, dates, big integers, decimals and that text, but stores every whole number as Int64. extendedJson: 'canonical' wraps every number too ($numberInt, $numberDouble, …), so a re-import keeps Int32/Int64/Double exactly — at the cost of larger files.
CSV stays plain: ids as text, dates as ISO text, a big integer as bare digits, arrays and objects as JSON text.
Large integers
An integer beyond ±(2^53 − 1) keeps every digit: in JSON/NDJSON it is a { "$numberLong": "…" } wrapper, in CSV bare digits, and the browser only passes the bytes on — it never calls JSON.parse on the file, so nothing is rounded.
Importing it again
Export as NDJSON and the import wizard sends that file back to the server as it is; the server honours the Extended JSON wrappers. Choose canonical when whole numbers must stay Int32. JSON arrays and CSV go through the wizard's own parser and type inference as before.
Memory and size
The bytes flow through a counting TransformStream into a Blob (new Response(stream).blob()), which the browser may keep on disk — the page does not hold the file as JavaScript strings or objects. Pick projection and filter to export less.
Related
useMongocampExport—rowsToCsv,downloadTextFileanddownloadBlobuseMongocampImport— the other direction
Server 2.3 – 2.5
withDeleted: trueexports the deleted documents of a soft-delete collection too (administrate grant).expand: ['customerId']expands declared references (JSON/NDJSON; ignored for CSV).- The result's
cappedistruewhen the server stopped atEXPORT_MAX_DOCUMENTS: NDJSON/CSV from the end marker ("capped":true/# end n capped, server 2.6); JSON has no marker — the headerx-export-max-documentsis set and the file holds exactly that many documents while more wereexpected(withoutexpected, a file at the limit counts as capped). Fewer documents than expected without that is not the limit (documents deleted meanwhile).documentsof a JSON file is counted while it streams (utils/jsonArrayCounter.ts).