Navigation

Import and Export Data

Note

Importing data into a collection is not permitted in MongoDB Compass Readonly Edition.

New in version 1.13.0

MongoDB Compass provides the ability to import and export data to and from your collections. Compass supports import and export for both JSON and CSV files. To import or export data to or from a collection, navigate to the detailed collection view by either selecting the collection from the Databases tab or clicking the collection in the left-side navigation.

Import Data into a Collection

MongoDB Compass can import data into a collection from either a JSON or CSV file.

Format Your Data

Before you can import your data into MongoDB Compass you must first ensure that it is formatted correctly.

When importing data from a JSON file, each document must exist on its own line in the file. Do not use commas at the end of lines to separate documents.

Example

The following .json file imports three documents:

{ "type": "home", "number": "212-555-1234" }
{ "type": "cell", "number": "646-555-4567" }
{ "type": "office", "number": "202-555-0182"}

MongoDB Compass automatically generates ObjectIDs for these objects on import since no ObjectIDs were specified in the initial JSON.

When importing data from a CSV file, the first line of the file must be a comma-separated list of your document field names. Subsequent lines in the file must be comma-separated field values in the order corresponding with the field order in the first line.

Example

The following .csv file imports three documents:

name,age,fav_color,pet
Jeff,25,green,Bongo
Alice,20,purple,Hazel
Tim,32,red,Lassie

MongoDB Compass automatically generates ObjectIDs for these objects on import since no ObjectIDs were specified in the initial CSV file.

Procedure

To import your formatted data into a collection:

  1. Navigate to the collection you wish to import data into. You can either select the collection from the Databases tab or click the collection in the left-hand pane.

  2. Click Collection in the top-level menu and select Import Data:


    Import data select menu

    MongoDB Compass displays the following dialog:


    Import data dialog
  3. Choose the appropriate file type and the location of the source data file.

  4. Click Import.

A progress bar displays the status of the import. If an error occurs during import, the progress bar turns red and an error message appears in the dialog. After successful import, the dialog closes and Compass displays the collection page containing the newly imported documents.

Export Data from a Collection

MongoDB Compass can export data from a collection as either a JSON or CSV file. If you specify a filter in the query bar prior to export, Compass can optionally only export documents which match the specified query.

Behavior

While it is possible to exclude documents by using a query filter, it is not possible to re-shape exported documents with a project document. Even when you specify a project option in the query, Compass still exports the entire document.

Procedure

To export collection data to a file:

  1. Navigate to the collection you wish to export data from. You can either select the collection from the Databases tab or click the collection in the left-hand pane.

  2. Click Collection in the top-level menu and select Export Collection:


    Export data select menu

    Compass displays the following dialog:


    Export data dialog

    The top section of the export dialog displays the query entered in the query bar prior to export which can be used to filter which collection documents are exported. If no query has been specified to the query bar, this section displays undefined.

  3. Use the Export Full Collection toggle to indicate whether to export only the documents matched by the query or the full collection.

    Note

    Having an undefined query filter and toggling the option to export the full collection both result in Compass exporting the full collection.

  4. Choose the appropriate file type and the location of the export file.

  5. Click Export.

A progress bar displays the status of the export. If an error occurs during export, the progress bar turns red and an error message appears in the dialog. After successful export, the dialog closes.

Example

The following example exports the entire test.people collection as a .json file called test-export.json:

Export data example

Note

The specified { age: 24 } query is ignored and the entire collection is exported because the Export Full Collection toggle is enabled.