• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:11 mins read
You are currently viewing Import CSV file using mongoimport in MongoDB

One of the ways to import data from CSV file into MongoDB is by using mongoimport command, this command allows you to import data from a variety of formats. This article will focus on the ways to import a CSV file using mongoimport command in MongoDB and explain it with different examples.

Advertisements

Related: Import JSON file Using mongoimport command in MongoDB

In this article, we will cover the basic options of the mongoimport command, as well as some advanced options for handling different types of data.

  • --type csv: type of file being imported as CSV.
  • --headerline: indicates the field titles will appear in the first row of the CSV file.
  • --db: indicates the name of the database where the CSV file will be imported.
  • --collection:  the name of the collection for importing the data of the CSV file.
  • --file: the path to the CSV file that you want to import.

1. Import a CSV file using mongoimport in MongoDB

Use the mongoimport command with --type as CSV to import a CSV file into MongoDB collection. This imports each records or line from a CSV file as a document into collection. It also has an option to specify the header using --headerline. In order to import a file from a specific directory use the --file option with the absolute file path.


# Add below data to File1.csv
name,age,email
John,25,[email protected]
Janney,20,[email protected]
Alex,26,[email protected]

# Import data from CSV file to collection
mongoimport --type csv --headerline --db MyFirstDatabase 
     --collection student --file C:\Users\Hp\Documents\File1.csv

The mongoimport in MongoDB is used here will import the data from the CSV file located C:\Users\Hp\Documents\File1.csv into the student collection of the MyFirstDatabase database. Also, the data will be imported into MongoDB as documents, with each row of the CSV file corresponding to a single document in the student collection. The following output shows that documents are successfully imported.

Import csv in MongoDB

Thus, the CSV content is inserted into the collection below.

mongoimport in MongoDB

2. Importing a CSV file with a Custom Fields in MongoDB

Moreover, we can import the data using the mongoimport option fields, the field option specifies the field names for the CSV file being imported. Also, the field names are separated by commas and must match the order of the columns in the CSV file.


# Add below data to File2.csv
name,age,email,course
John,25,[email protected],MongoDB
Janney,20,[email protected],Python
Alex,26,[email protected],Java

# Importing data using fields option
mongoimport --type csv --fields name,age,course,city 
            --db MyFirstDatabase --collection student2 
             --file C:\Users\Hp\Documents\File2.csv

For example, the following mongoimport command in MongoDB will import the content of the CSV file placed C:\Users\Hp\Documents\File2.csv into the student2 collection of the MyFirstDatabase database. Moreover, the field names for the CSV file are name, age, course, and city, which must match the order of the columns in the CSV file.

Import csv in MongoDB

The student2 collection now looks like this after importing the CSV content.

mongoimport in MongoDB

3. Importing with Nested Objects

Additionally, the mongoimport command in MongoDB will be used to import the CSV data which contains nested objects. For example, the below command of mongoimport will help us to import the data of the CSV file into the student3 collection.


# Add below data to File3.csv
name,age,address,admission
John,25,[city:Chicago,street:ABC}]
Janney,20,[{city:San Diego,street:XYZ}]
Alex,26,[{city:Los Angeles,street:LMN}]

# Import CSV data having nested objects
mongoimport --type csv --fields name,address.city,address.street 
            --db MyFirstDatabase --collection student3 
            --file C:\Users\Hp\Documents\File3.csv

For example, the above command of mongoimport will help us to import the data of the CSV file into the student3 collection. Here, the field names for the CSV file are name, address.city, and address.street, which indicate that a address field is a nested object that contains city and street fields. The following output indicates that documents of CSV are imported.

Import csv in MongoDB

Now, the collection student3 is updated with the imported data of the CSV file.

mongoimport in MongoDB

4. Importing a with a custom batchsize

Next, we used the option batchSize of the mongoimport command in MongoDB. The batchSize specifies the number of documents to import in each batch. The default value given by MongoDB is 1000.


# Add below data to File4.csv
name,age,email,date
John,25,[email protected],23-04-2022
Janney,20,[email protected],16-02-2023
Alex,26,[email protected],-1-03-2021

# Usage of batchSize of the mongoimport command
mongoimport --db MyFirstDatabase --collection student4 --type csv 
            --headerline --batchSize 100 
            --file C:\Users\Hp\Documents\File4.csv

Consider the example given above where the mongoimport command will import the content of the file path into the student4 collection. The batchSize option used in this command specifies the documents should be imported in batches of 100 at a time, which can be useful for importing large amounts of data. Thus, the output displayed the results of successfully importing the content of CSV.  

Import csv in MongoDB

As we have three fields in the CSV file so only those are shown in the collection. But we can add up to 100 documents in the CSV file and then import them into the MongoDB collection by following the above query.

mongoimport in MongoDB

5. Importing a CSV file with an upsert option using mongoimport command

In some cases, we want to update document if exists or insert docuemnt if not exist. for this, the mongoimport command provides an upsert option.

The --upsert option specifies that an update operation should be performed if the document already exists in the collection. The --upsertFields option specifies the fields that should be used to match the existing document during an upsert operation.


# Add below data to File4.csv
marks,grade
98,A
78,B
90,C

# Usage of --upsert option 
mongoimport --db MyFirstDatabase --collection student4 --type csv 
            --headerline --upsert --upsertFields marks,grade 
            --file C:\Users\Hp\Documents\File4.csv

Consider the mongoimport command which will import the data of the specified CSV file to the collection student4. Here, the matching fields are marks and grade. If a document with the same marks and grade values already exist in the collection, it will be updated with the new data otherwise it inserts.

Import csv in MongoDB

The following fields are now upserted into the collection student.

mongoimport in MongoDB

6. Conclusion

In conclusion, to import data into MongoDB from a CSV file use the mongoimport command. Whether it’s a large amount of data or just a few records, mongoimport provides a simple and efficient way to do it. By using this command you can also choose the columns from CSV to import, bulkimport, e.t.c

More details about this topic can be found here.