Import CSV file to MongoDB

  • Read
  • Discuss

There are two ways of importing CSV file to MongoDB:

  1. Using MongoShell (Mongosh)
  2. Using MongoDB Compass

Import CSV File using MongoShell:

In MongoDB, we can import csv file into two ways:

  • With header row
  • Without header row
  1. With header row: We can import data with header row with the help of –header that shows the name of the fields will use the first line of the CSV file.

Syntax:

mongoimport --db database_name --collection collection_name --file file_location --type csv --headerline

  1. Without header row: We can import data without header row by excluding –header. But in the place of the header, we have to put –fields that show the name of the field that we want to give. Field names are separated by a comma.

Syntax:

mongoimport --db database_name --collection collection_name --file file_location --type csv --field field_names

Steps:

To import CSV file you need to follow the following steps:

Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don’t close this cmd to stay connected to the server.

Step 2: Open one more command prompt window and direct it to the bin folder of your MongoDB Server and now you are ready to import files in mongoDB database.

Use the following command to import the json file into mongoDB.

mongoimport --jsonArray --db PetDB --collection Pets --file file_location --headerline

Now, open the mongo shell window and check for the imported data:

  • First, connect to the mongoDB server
  • Then run these commands to check for imported data

Note: If the collection name is not specified then the collection name is created based on the first name of the file. Using these steps you can also import TSV files, simply using –type tsv.

Import CSV File using MongoDB Compass:

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.

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.

Steps:

To import your formatted data into a collection follow these steps:

  1. Connect to the deployment containing the collection you wish to import data into.
  1. Navigate to your target collection.

You can either select an already created database or you can create a new one. Similarly, you can select the collection from the Collections tab or click the collection in the left-hand pane.

  1. Click the Add Data dropdown and select Import File.

MongoDB Compass will show the following dialog:

  1. Select the location of the source data file under Select File.
  2. Choose the appropriate file type.

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.

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.

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

Leave a Reply

Scroll to Top