Skip to content

Import & Export ​

Export ​

GET /mongodb/collections/{name}/documents/export (read grant) streams the documents of a collection. Every document is written when the database delivers it, so the size of the collection does not matter for the memory of the server. A client that stops reading stops the export.

Parameter
formatndjson (default, one document per line), json (one array) or csv
filtera Lucene filter
sort, limitlike the list (-field sorts descending), and the largest number of documents
projectionthe fields to export. For csv these are the columns in this order
extendedJsonrelaxed (default for json/ndjson), canonical or plain (default for csv)

POST …/documents/export takes filter, sort, projection and limit in the body (no URL limit); format and extendedJson stay query parameters.

Content-Disposition: attachment; filename="name.ndjson" names the file.

In CSV the first line is the header and lines end with \r\n. A cell with a comma, a quote or a line break is quoted (quotes doubled), null is an empty cell, and nested values are JSON text. Without projection the columns are the fields of the first 1000 documents (_id first).

A wrong parameter answers 400 before anything is written. A failure during the stream can only end it, because the status 200 was already sent.

End marker ​

With trailer=true a cut-off export can be told from a complete one:

FormatCompleteFailed
ndjsonlast line {"$end":true,"count":n}last line {"$error":"…"}
csvlast line # end nlast line # error …
jsonends with ]ends without ]

Without trailer the export has no marker; a json export can still be checked for its ].

When EXPORT_MAX_DOCUMENTS cut the export (2.6), the end marker says so: {"$end":true,"count":n,"capped":true} / # end n capped. While the limit is on, every export has the header x-export-max-documents: n (also for json, which has no end marker); a json array of exactly n documents may be capped.

Import ​

POST /mongodb/collections/{name}/documents/import (write grant) stores NDJSON from the request body, which is the format the export writes. The body is read as a stream.

text
{"_id": "a", "n": 1}
{"_id": {"$numberInt": "7"}, "when": "2024-06-15T12:00:00Z", "text": {"$string": "2024-06-15T12:00:00Z"}}
  • A line is read like the body of an insert: wrappers, dates and $string work, and a document without _id gets an ObjectId. Blank lines are skipped, and lines end with LF or CRLF.
  • A canonical export (extendedJson=canonical) re-imports with the same types.
  • Documents are stored in batches of 1000. A line that cannot be stored (not JSON, an invalid wrapper, a duplicate key) is reported, and the valid lines are still stored:
json
{
  "read": 4, "inserted": 3, "failed": 1,
  "errors": [{ "index": 5, "code": 4008, "message": "the line is not valid JSON: ..." }],
  "errorsTruncated": false
}

index is the 1-based line number (blank lines count). code is 4008 for a line that is not a document, or the database error code (11000 for a duplicate key). At most 1000 errors are listed.

All or nothing ​

When an import is aborted, the documents of the earlier batches are deleted again and x-error-additional-info says so. An import is aborted when the body is larger than IMPORT_MAX_BYTES (413), a line is larger than 17 MB (400), or the database or connection fails. Send the whole body again in that case.

The rollback deletes documents; it is not a transaction, so readers can see them until the abort.

Released under the MIT License.