Skip to content

Import and export collections

  • MongoDB

The Export dialog writes collections, or the documents a collection tab’s query finds, to files. The Import dialog reads a file into a collection. Both run as jobs in the job runner, with progress and Cancel, so the size of the collection does not matter. Extended JSON and BSON files keep every BSON type.

  1. Open the Export dialog:
    • Export Collection… from a collection’s menu in the explorer,
    • Export Collections… from a database’s or a Collections folder’s menu, with every collection of the database to choose from,
    • Export… in a collection tab’s result toolbar,
    • or Export in the toolbar.
  2. Under Documents, tick the collections (Select all and Select none help). From a collection tab, choose What the query finds or Every document of the collection instead.
  3. Under Format, pick the format and its options (see below).
  4. Under Destination, tick Compress with gzip if you want a .gz file, then choose Choose file…. Several collections go to a folder, a file each: Choose folder….
  5. Choose Export.
Format What it writes Options
JSON An array of documents in Extended JSON Relaxed or Canonical; Indent each document
JSON Lines One document per line, as mongoexport writes them Relaxed or Canonical
CSV A column per field, for spreadsheets; sub-document fields as columns such as address.city Delimiter, Field names in the first row, and the fields
BSON Documents back to back, as mongodump’s .bson files None
SQL An INSERT statement per document, a column per field Table, Create the table first, its column types read from the documents, and the fields

Relaxed, the default, writes numbers and dates as plain JSON where nothing is lost. A value that plain JSON would change, such as an Int64 beyond 253, stays in its canonical form. Canonical spells out every type, so Int32, Int64 and Double stay apart when the file is read back.

Type the columns into Fields, one per line as dotted paths, such as customer.name. Left empty, the columns are the fields of the first 1,000 documents. For a single collection, Scan fields fills the list from a sample of its documents.

Numbers, strings, booleans, ISO dates, ObjectId hex and UUIDs are written as text; arrays and other types as relaxed Extended JSON.

In SQL, the table is the collection’s name, or the Table you type when exporting one collection. Numbers, booleans and dates are SQL literals, and table and column names are double-quoted. Create the table first, its column types read from the documents is ticked to start with: the CREATE TABLE takes its column types from the first 1,000 documents, with BIGINT, DOUBLE PRECISION or NUMERIC for numbers, BOOLEAN, TIMESTAMP, and TEXT for anything else or a mix of types.

Schedule… beside Export sets the same export up to run on a schedule instead. The schedule keeps the export without its path, and each run writes a new file, or folder, named from the schedule’s file name template.

  1. Open the Import dialog:
    • Import into Collection… from a collection’s menu,
    • Import Collection… from a database’s or a Collections folder’s menu, for a new collection named after the file,
    • Import… in a collection tab’s result toolbar,
    • or Import in the toolbar.
  2. Choose Choose file… and pick a JSON, JSON Lines, CSV or BSON file, gzipped or not. The Format follows the file’s extension; change it if it is wrong.
  3. For CSV, set the options (see below).
  4. Under Into, check the Collection and pick the mode.
  5. Under When a document is refused, choose Stop there (the documents before it stay imported) or Skip it and go on (the job lists each one skipped).
  6. Choose Import.
Mode What it does
Insert Adds the documents; one whose _id is taken is an error
Upsert by _id Replaces the stored document with the same _id, or adds it; a document without an _id is added
Replace the documents Deletes every document of the collection first; its indexes and validator stay

A JSON file can hold an array, one document per line, or documents one after another, so files that mongoexport writes, pretty-printed ones included, read without a choice to make. mongodump’s .bson files read too; its metadata.json does not, so indexes and options are not imported. The documents are written in ordered batches of 1,000 documents or 8 MB.

  • Delimiter and Field names in the first row, as in the export.
  • Read numbers, booleans, dates and JSON as their types turns whole numbers into Int32 (or Int64 or Double when they do not fit), decimals into Double, true and false into booleans, ISO dates into dates, a 24-digit hex _id into an ObjectId, and text that parses as Extended JSON into that document or array. Dotted field names such as address.city make sub-documents.
  • Keep every value as text reads every value as a string.
  • Empty fields: Leave the field out, Null or An empty string.

Documents Querybara 0.1.19 · built from89dc752