How to Import CSV Files in MongoDB

Bilal Shahid Feb 02, 2024
  1. CSV File
  2. Use the mongoimport Command to Import CSV Files in MongoDB
  3. Conclusion
How to Import CSV Files in MongoDB

The article is specially curated to help people with the mongoimport command that allows users to import CSV files to the database in MongoDB.

The article describes what CSV files are. An example of a CSV file is provided in the beginning to help with the article later.

The example of a CSV file is used to help understand the mongoimport command. This command is used to import files to the database in MongoDB.

Follow through the article to understand the mongoimport command and how to properly import CSV files to the MongoDB database. Everything is described in detail to help the user out.

CSV File

This section is dedicated to thoroughly understanding the concept of CSV files. A CSV file allows the user to import data from a text file to the database.

CSV is an abbreviation of comma-separated values. CSV files are text files that contain data separated by commas.

This allows the user to store data in a table format that MongoDB can later use to store the values in the respective columns in the database.

Difference Between CSV and XLS Files

Excel files perform the same function as CSV files. However, CSV files are text files that store data separated by commas.

On the other hand, XLS files represent an Excel sheet with a binary file format to hold information about all the worksheets. This includes storing information about both the content and the formatting.

Example of a CSV File

To help understand the mongoimport command better, we will use an example of a CSV file. This section shows the contents of the CSV file.

The later section explores how to import the contents of this CSV file into the database.

Let’s assume that the following CSV file is named EmployeeData.csv in your system, and it has the following contents:

Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
James Robert,976 Austin Secret Lane,Roosevelt,Utah,84066
William Sophia,1704 Cooks Mine Road,Albuquerque,New Mexico,87109

The mongoimport command will import this CSV file to the database.

Use the mongoimport Command to Import CSV Files in MongoDB

This section highlights the use of the mongoimport command. Taking the EmployeeData.csv example CSV file mentioned in the section above to execute the mongoimport command.

Here is a depiction of the use of the mongoimport command.

$ cat > EmployeeData.csv
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
James Robert,976 Austin Secret Lane,Roosevelt,Utah,84066
William Sophia,1704 Cooks Mine Road,Albuquerque,New Mexico,87109
 ctrl-d
$ mongoimport -d mydb -c things --type csv --file EmployeeData.csv --headerline
connected to: 127.0.0.1
imported 4 objects
$ mongo
MongoDB shell version: 1.7.3
connecting to: test
> use mydb
switched to db mydb
> db.things.find()
{ "_id" : ObjectId("4d32a36ed63d057130c08fca"), "Name" : "Jane Doe", "Address" : "123 Main St", "City" : "Whereverville", "State" : "CA", "ZIP" : 90210 }
{ "_id" : ObjectId("4d32a36ed63d057130c08fca"), "Name" : "James Robert", "Address" : " 976 Austin Secret Lane", "City" : "Roosevelt", "State" : "Utah", "ZIP" : 84066}
{ "_id" : ObjectId("4d32a36ed63d057130c08fcb"), "Name" : "William Sophia", "Address" : "1704 Cooks Mine Road", "City" : "Albuquerque", "State" : "New Mexico", "ZIP" : 87109}

The first cat command mentioned in the example above is used to display the contents of a file with the name provided in the command.

This is an optional command to check out the contents of the file mentioned. It helps to ensure that the file exists with the required content.

The next command is the main mongoimport command that imports data from the CSV file to the database. The format of the command is as follows:

mongoimport <options> <connection-string> <file>

The extended format of the mongoimport command can be viewed below.

mongoimport --db DB_Name --collection Collection_Name --type csv --file File-Name-to-Import --headerline

The description of each of the arguments with the mongoimport command can be written as mentioned in the extended format above or the example provided above.

  1. The db argument takes the name of the database that contains the collection.
  2. The collection argument takes the name of the collection.
  3. The type argument specifies the type of file that is being imported.
  4. The file argument takes the file’s name that must be imported.
  5. The headerline argument specifies the mongoimport command that the first line in the file contains the field names.

After using the mongoimport command with the specific arguments, the message displays how many objects are imported to the database.

After that, a mongo command is used to connect to MongoDB, and then the use command is written along with the name of the database to connect to it.

The db.things.find() function displays the objects imported from the file to the database. This command displays all the imported objects individually.

Use Authentication When Importing CSV File

It is possible to authenticate the CSV file while importing it. Some arguments must be added to extend the mongoimport command to allow authentication.

Here is the format that allows authentication of a CSV file while importing it.

d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'

Conclusion

MongoDB is a cross-platform database program that is document-oriented. It uses JSON-like documents that come with optional schema.

It is a NoSQL database program that helps make the backend storage for the programs easier than before.

MongoDB allows users to import data from different files, such as the XLS or CSV file. The article thoroughly describes how to import data from a CSV file to MongoDB using the mongoimport command.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub

Related Article - MongoDB CSV