JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Importing CSV files in MongoDB

Author:JIYIK Last Updated:2025/04/28 Views:

This article is dedicated to helping you use the mongoimport command, which allows users to import CSV files into a database in MongoDB.

This article explains what a CSV file is. An example CSV file is provided at the beginning to help you read the article later.

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

Read this article to learn about the mongoimport command and how to properly import a CSV file into a MongoDB database. Everything is described in detail to help the user.


CSV file

This section is dedicated to a thorough understanding of the concept of CSV files. CSV files allow users to import data from text files into a database.

CSV is the abbreviation of Comma Separated Values. A CSV file is a text file that contains data separated by commas.

This allows users to store data in a tabular format that MongoDB can later use to store the values ​​in individual columns of the database.

Differences 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 Excel worksheets with a binary file format that saves information about all worksheets. This includes storing information about the content and formatting.

CSV file example

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

The following sections discuss how to import the contents of this CSV file into a database.

Assume that the following CSV file is named EmployeeData.csv in your system and contains the following content:

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 into the database.


Import CSV files into MongoDB using mongoimport command

This section focuses on the usage of the mongoimport command. Execute the mongoimport command on the EmployeeData.csv sample CSV file mentioned in the previous section.

Following is a description of the usage 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 above example is used to display the contents of the file with the name provided in the command.

This is an optional command that checks the contents of the mentioned file. It helps to ensure that the file exists and contains the required content.

The next command is the main mongoimport command, which imports data from a CSV file into the database. The command format is as follows:

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

The expanded format of the mongoimport command can be seen below .

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

The description of each parameter of the mongoimport command can be written following the expanded format above or as described in the example provided above.

  1. The db parameter takes the name of the database containing the collection.
  2. The collection parameter takes the name of a collection.
  3. The type parameter specifies the type of file to import.
  4. The file parameter takes the name of the file that has to be imported.
  5. The headerline parameter specifies the first line in the file that contains the field names for the mongoimport command.

After using the mongoimport command with specific parameters , this message shows how many objects were imported into the database.

After that, connect MongoDB using the mongo command and then write the use command along with the database name to connect to it.

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

Use authentication when importing CSV files

It is possible to authenticate a CSV file while importing it. You must add some parameters to extend the mongoimport command to allow authentication.

This is the format that allows authentication of the CSV file when it is imported.

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

Summarize

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

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

MongoDB allows users to import data from different files, such as XLS or CSV files. This article describes in detail how to use the mongoimport command to import data from a CSV file into MongoDB.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Date comparison in MongoDB

Publish Date:2025/04/28 Views:169 Category:MongoDB

This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie

Find objects between two dates in MongoDB

Publish Date:2025/04/28 Views:94 Category:MongoDB

In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio

Comparing Dates in MongoDB

Publish Date:2025/04/28 Views:115 Category:MongoDB

Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th

Convert string to date in MongoDB

Publish Date:2025/04/28 Views:162 Category:MongoDB

MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.

Building the MongoDB REST API

Publish Date:2025/04/28 Views:70 Category:MongoDB

MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD

Using ORM with MongoDB

Publish Date:2025/04/28 Views:117 Category:MongoDB

MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general

Locking mechanism in MongoDB

Publish Date:2025/04/28 Views:95 Category:MongoDB

In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock

Unique Index in MongoDB

Publish Date:2025/04/28 Views:130 Category:MongoDB

In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo

Creating Indexes in MongoDB

Publish Date:2025/04/28 Views:178 Category:MongoDB

Indexes help resolve queries efficiently. Without indexes, MongoDB must iterate through every document in the collection to find the documents that match the query. It will waste time and require MongoDB to handle such information. Therefor

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial