Navigation

    Import and Export Data

    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.

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

    • 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.

    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
    Beaker IconExample

    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.

    To import your formatted data into a collection:

    1

    To learn how to connect to a deployment, see Connect to MongoDB.

    2

    You can either select the collection from the Collections tab or click the collection in the left-hand pane.

    3
    Add data button

    Compass displays the following dialog:

    Import data dialog
    4
    5

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

    If you are importing a CSV file, you may specify fields to import and the types of those fields under Specify Fields and Types. The default data type for all fields is string.

    Select fields to import from a CSV file

    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.

    6

    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.
    7

    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.

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

    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.

    To export an entire collection to a file:

    1

    To learn how to connect to a deployment, see Connect to MongoDB.

    2

    You can either select the collection from the Collections tab or click the collection in the left-hand pane.

    3
    Export data select menu

    Compass displays the following dialog:

    Export data dialog, step 1

    The export dialog initially displays the query entered in the query bar prior to export, if applicable. If no query was specified, this section displays a find operation with no parameters, which returns all documents in the collection.

    To ignore the query filter and export your entire collection, select Export Full Collection and click Select Fields.

    4

    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.

    Export data dialog, step 2
    5

    Under Select Export File Type, select either JSON or CSV. If you select JSON, your data is exported to the target file as a comma-separated array.

    Then, under Output, choose where to export the file to.

    Export data dialog, step 3
    6

    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 import and export data from the command line, you can use MongoDB's Database Tools. See mongoimport and mongoexport.

    Give Feedback