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
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 of the source data file.

Under Select Input File Type, select either JSON or CSV.

4

Select the location of the source data file under Select File.


5

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
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, if applicable. Use this query to filter which collection documents are exported. If no query was specified, 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

Compass exports the full collection both when the query filter is undefined and when you enable the Export Full Collection option.

4

Choose the appropriate file type.

Under Select Output File Type, select either JSON or CSV.

5

Select the location of the export file under Select File.


6

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.