Import and Export Data
On this page
You can use MongoDB Compass to import and export data to and from 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.
Note
Compass is not a tool for backing up data. For information on backup solutions, see Backup Methods for a Self-Managed Deployment.
Import Data into a Collection
MongoDB Compass can import data into a collection from either a JSON or CSV file.
Limitations
Importing data into a collection is not permitted in MongoDB Compass Readonly Edition.
Importing data is not available if you are connected to a Data Lake.
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, you can format your data as:
Newline-delimited documents, or
Comma-separated documents in an array
Example
The following newline-delimited .json
file is formatted
correctly:
{ "type": "home", "number": "212-555-1234" } { "type": "cell", "number": "646-555-4567" } { "type": "office", "number": "202-555-0182"}
The following comma-separated .json
array file is also
formatted correctly:
[{ "type": "home", "number": "212-555-1234" }, { "type": "cell", "number": "646-555-4567" }, { "type": "office", "number": "202-555-0182"}]
Compass ignores line breaks in JSON arrays.
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:
Connect to the deployment containing the collection you wish to import data into.
To learn how to connect to a deployment, see Connect to MongoDB.
Navigate to your target collection.
You can either select the collection from the Collections tab or click the collection in the left-hand pane.
Select the appropriate file type.
Select either a JSON or CSV file to import and click Select.
If you are importing a CSV file, you may specify fields to import and the types of those fields under Specify Fields and Types.
To exclude a field from a CSV file you are importing, uncheck the checkbox next to that field name. To select a type for a field, use the dropdown menu below that field name.
Configure import options.
Under Options, configure the import options for your use case.
If you are importing a CSV file, you may select how your data is delimited.
For both JSON and CSV file imports, you can toggle Ignore empty strings and Stop on errors:
If checked, Ignore empty strings drops fields with empty string values from your imported documents. The document is still imported with all other fields.
If checked, Stop on errors prevents any data from being imported in the event of an error. If unchecked, data is inserted until an error is encountered and successful inserts are not rolled back. The import operation will not continue after encountering an error in either case.
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. To see all errors, click View Log.
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 or aggregation pipeline for your collection, Compass only exports documents which match the specified query or pipeline results.
Behavior
Avoid exporting to CSV files when possible. CSV files may lose type information and are not suitable for backing up your data.
You can use a query filter to export only the documents that match the filter.
You can use the Project field in the query bar to specify the fields to return or export.
Procedure
To export an entire collection to a file:
Connect to the deployment containing the collection you wish to export data from.
To learn how to connect to a deployment, see Connect to MongoDB.
Navigate to your desired collection.
You can either select the collection from the Collections tab or click the collection in the left-hand pane.
Select your file type.
You can select either JSON or CSV. If you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:
JSON Format | Description |
---|---|
Default Extended JSON | A string format that avoids any loss of BSON type information. This
is the default Compass setting. |
Relaxed Extended JSON | A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information. WARNING: This format is not recommended for data integrity. |
Canonical Extended JSON | A string format that emphasizes type preservation at the expense of
readability and interoperability. That is, conversion from canonical
to BSON will generally preserve type information except in certain
specific cases. |
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.
To export a subset of documents from a collection:
Connect to the deployment containing the collection you wish to export data from.
To learn how to connect to a deployment, see Connect to MongoDB.
Navigate to your desired collection.
You can either select the collection from the Collections tab or click the collection in the left-hand pane.
Select document fields to include in your exported file.
You can choose to export All Fields or Select fields in table. In the query bar, you can also use the Project field to specify the fields to return or export.
Only fields that are checked are included in the exported file.
You can add document fields to include with the Add Field button if the field you want to include is not automatically detected.
Note
Compass samples your collection to pre-populate a list of fields. Fields which only appear in a small percentage of documents may not be automatically detected.
For details on sampling, see Sampling.
Select the appropriate file type.
Under Export File Type, select either JSON or CSV. If you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:
JSON Format | Description |
---|---|
Default Extended JSON | A string format that avoids any loss of BSON type information. This
is the default Compass setting. |
Relaxed Extended JSON | A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information. WARNING: This format is not recommended for data integrity. |
Canonical Extended JSON | A string format that emphasizes type preservation at the expense of
readability and interoperability. That is, conversion from canonical
to BSON will generally preserve type information except in certain
specific cases. |
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.
To export results from your aggregation pipeline:
Connect to the deployment containing the collection you wish to export data from.
To learn how to connect to a deployment, see Connect to MongoDB.
Navigate to your target collection.
You can either select the collection from the Collections tab or click the collection in the left-hand pane.
Create an aggregation pipeline and run for results.
To learn how to create an aggregation pipeline, see Create an Aggregation Pipeline.
Select the appropriate file type.
Under Export File Type, select either JSON or CSV. If you select JSON, you can expand the Advanced JSON Format dropdown and select from the following extended JSON formats:
JSON Format | Description | Sample Document | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Default Extended JSON | A string format that avoids any loss of BSON type information. This
is the default Compass setting. |
| |||||||||||
Relaxed Extended JSON | A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information. WARNING: This format is not recommended for data integrity. |
| |||||||||||
Canonical Extended JSON | A string format that emphasizes type preservation at the expense of
readability and interoperability. That is, conversion from canonical
to BSON will generally preserve type information except in certain
specific cases. |
|
Import and Export Data from the Command Line
To import and export data from the command line, you can use MongoDB's Database Tools. See mongoimport and mongoexport.