Import CSV into MongoDB – Multiple Ways

In this MongoDB tutorial, We are going to learn “How to import CSV file into MongoDB”. We will also cover this with different operations. These are the following topics that we are going to cover in this tutorial:

  • Import CSV into MongoDB
  • Import CSV into MongoDB using compass
  • Import CSV into MongoDB using atlas
  • Import CSV into MongoDB using python

The CSV stands for comma-separated values. It is a plain text file that includes a list of data. We import the CSV file into MongoDB that will help the developer to easily import for reading the content inside it.

Import CSV into MongoDB

In MongoDB, If you want to import the data then MongoDB provides a special tool that is known as mongoimport. With the help of this tool, you can import JSON, CSV, or TSV into the MongoDB database.

Generally, the tool mongoimport is present in the bin folder of MongoDB but if it is not available there then you to install it into your system.

For downloading the mongoimport tool, you have to download the MongoDB database tools .zip file from here.

How to download mongoimport tool
Download MongoDB Database Tools

After downloading the file you have to follow the below steps:

  • After downloading the zip file, unzip it.
  • Now goto bin folder and copy all.exe files and paste all the file into MongoDB bin folder.
Download mongoimport tool
Downloaded MongoDB database tools
Download mongoimport in MongoDB
Store all tools in the MongoDB bin folder
  • We stored all.exe files into the right path and now you can use the mongoimport tool.

We can import CSV files 2 ways in MongoDB:

  1. With header row
  2. Without header row
  • With header row: You can import data with header row with the help of -header. It shows the name of the fields in the first line of the CSV file.

Syntax:

mongoimport –db database_name –collection collection_name –type csv –file file_location –header

Example:

import CSV into MongoDB
demo.csv

This demo.csv file we are going to import into the MongoDB database.

Import data from CSV file using below command:

mongoimport -–db importdata –-collection data –-type csv -–file E:\data.csv –-headerline
import CSV file into MongoDB database
Import CSV file into MongoDB database

As you can see, we successfully import the CSV file into the MongoDB database. Now, you can also check the data in the database.

import CSV file into MongoDB
Data after importing CSV file into MongoDB

Here, the importdata, data are the database and collection respectively and see the CSV file data is also imported into data collection.

  • Without header row: By excluding the -header, you can import data without the header row. But, you have to put -field in the place of header. The -field will show the name of the field that you want to give and you have to separate it by a comma.

Syntax:

mongoimport –db database_name –collection collection_name –type csv –fields field_names –file file_location

Example:

MongoDB import CSV
test.csv

This test.csv file we are going to import into the MongoDB database.

Use the below command to import CSV into the database:

mongoimport –db importdata –collection test –type csv –fields ,A,B,C,D –file E:\test.csv
MongoDB import CSV into database
MongoDB Import CSV into database

We successfully import the CSV file into the MongoDB database. Here, A, B, C, D are the field names, check into your CSV file and write accordingly.

Now, If you want to check the data of the CSV file is stored or not then start the MongoDB server. With the help of the find() method, you can access the data of the CSV file that you import into the database.

Data after importing CSV into MongoDB

In the output, See the data(CSV file) is successfully imported into data collection.

Read: MongoDB group by multiple fields

Import CSV into MongoDB using compass

In MongoDB, If you want to import the CSV file using compass then you don’t require any tool like earlier we used mongoimport. You have to simply use the MongoDB compass (GUI) and you can easily import the CSV file and also perform various operations.

For importing the CSV file you have to follow the following steps:

  • Open MongoDB compass
  • Create a new database and collection
create a new collection in MongoDB compass
create a new collection in MongoDB compass

Here, I created a new collection, enterprise in organisation database. You can write any name for the collection and simply click on Create Collection button.

  • Now enterprise Collection is created.
  • Select the created collection and then click on ADD DATA button. It will open a drop-down list and then click on Import File.
  • This will open a new tab.
Import CSV into MongoDB using compass
Import CSV into MongoDB using compass
  • Now, you have to select File that you want to import and and File Type like CSV or JSON.
Import CSV into MongoDB compass
Import CSV into MongoDB compass

Here, we select the CSV file and then select the file type CSV as we are importing the CSV file. After that click on IMPORT Button.

  • Now, we Successfully IMPORT the CSV file into collection.
import CSV into compass
Import CSV into MongoDB using compass

Here, you can see all the data inserted into the collection. This is the simplest way to import the data into the collection using MongoDB compass.

Note that, In MongoDB compass also you can perform aggregation operations and many other things as well.

Read: How to store images in MongoDB

Import CSV into MongoDB using atlas

As you know Atlas is MongoDB database-as-a-service. We use MongoDB atlas for backs up data using the native capabilities of the cloud provider.

It provides various platforms where you can deploy like Amazon Web Services(AWS), Google Cloud Platform(GCP), and Microsoft Azure. The service will offer pay-as-you-go pricing.

To import the CSV file into MongoDB using the atlas, you have to use the mongoimport tool.

you have to follow the below steps to import the CSV file:

  • Login to your MongoDB atlas account.
  • If you don’t have an account, create a new one click.
Create a new account on MongoDB atlas
Create a new account on MongoDB atlas

Here, after clicking on the above link. It will open this for you and you have to simply write your detail and your will account create successfully.

Login into MongoDB atlas
Login into MongoDB atlas

When you first-time log in, this is the interface of MongoDB atlas.

  • Now you have to create a project.
Create a new project in MongoDB atlas
Create a new project

Here, you have to click on Project 0 drop-down menu and click on the New Project button. This will open a new window

  • Write the project name ImportData and click on Next button.
create new project in atlas
create a new project in the atlas
  • Now it will ask you to Add Member, if you want to then add otherwise click on Create Project button.
Create project in MongoDB atlas
Add Members to the project
  • Build a Database
  • After click on build database button deploy a cloud database.
Deploy a cloud database on atlas
Deploy a cloud database on atlas
  • Here, select a shared cloud database that is free for you so Create it.
Create a shared cluster in atlas
Create a shared cluster in the atlas

Here, we are using AWS (AWS, N. Virginia (us-east-1) ) service that is free for you. But I recommend you to read all the information carefully.

  • Click on Create Cluster button.
  • It will take 1-3 min for creating the cluster so you have to wait.
  • After successfully creating cluster fill all the detail for deployment of cluster.
deployment of database in atlas
Database Deployments
  • Now create the database username and password and add IP address.
Create database user and set IP in atlas
Create a database user and set add IP address in the atlas

Here, we set an IP address that can access anywhere. You can also create your personal IP address. After that create a database user (test123) and set a password and click on Create Database User button.

  • you have Choose a connection method, here I am doing on MongoDB shell
  • Now click on cluster name Cluster 0 and select the primary REGION.
Import CSV into MongoDB using atlas
Select the primary REGION of the cluster
  • This will provide the full host name that we use further in MongoDB shell to import CSV file.
Full Host Name
  • Now, we import the CSV file into database, for this we use mongoimport tool.
  • Open the command prompt and use the below command to import CSV file
mongoimport --host cluster0-shard-00-01.mrbxt.mongodb.net:27017 --db starwars --collection test --type csv --drop --fields ,A,B,C,D --file E:\test.csv --authenticationDatabase admin --ssl --username test123 --password test123
Import CSV file into MongoDB atlas
Import CSV file into MongoDB atlas

Here first, we write the mongoimport (tool) for importing the CSV file then provide the full hostname. After that provide the Database and collection name starwars, test respectively.

When you are importing CSV you have to provide the fields name as well as per given in CSV file like here (A, B, C, D). After that provide the authenticationDatabase that is always admin and then the username and password that we created earlier.

  • If you want to see the data of CSV file into database then check it.
  • Start the MongoDB server using below command.
mongo "mongodb+srv://cluster0.mrbxt.mongodb.net/myFirstDatabase" --username test123
Start atlas MongoDB Server
Start atlas MongoDB Server

Here, after executing the command, It will ask you to enter the password then it starts the server.

  • Now check the data inside the database.
Import CSV into MongoDB atlas
Import CSV into MongoDB atlas

Here, you see CSV file data is successfully imported into the MongoDB database.

Read: MongoDB group by count 

Import CSV into MongoDB using python

First of all, I say storing the CSV file in MongoDB using python would be a bad idea because it is impractical to manipulate and query the data in that format.

But you still want to import then convert it into JSON documents to add it to the MongoDB database.

Example:

We took an example to import CSV files into MongoDB using Python.

import csv
import json
import pandas as pd
import sys, getopt, pprint
from pymongo import MongoClient

#CSV to JSON Conversion
csvfile = open('E:/Sample.csv', 'r')
reader = csv.DictReader( csvfile )
mongo_client=MongoClient() 

db=mongo_client.DataImport
db.sample.drop()

header= [ "Serial Number", "Company Name", "Employee Markme", "Description", "Leave"]
for each in reader:
    row={}
    for field in header:
        row[field]=each[field]

    db.sample.insert(row)

In this Python code, we import some libraries as CSV, JSON, pandas and pymongo. Here, pandas are used to read the CSV file and pymongo to make the connection between Python and MongoDB databases.

After that, we define all the columns of CSV into a list and store the list into a variable(header). Use insert() method to store data into database with the help of for loop.

Import CSV into MongoDB using python
Import CSV into MongoDB using python

After executing the above code, If you want to check the data of the CSV file then start the MongoDB server and check it.

Import CSV file into MongoDB using python
Import CSV file into MongoDB using python

Here, you can see the database and collection name, DataImport and Sample respectively created successfully and data is also stored in the collection. This way you can simply store the CSV file into a database using python.

You may also like reading the following MongoDB tutorials.

In this tutorial, you have learned How to import CSV into MongoDB in different ways and also covered in detail how to start MongoDB atlas to import CSV. These are the following topics that we covered in this tutorial:

  • Import CSV into MongoDB
  • Import CSV into MongoDB using compass
  • Import CSV into MongoDB using atlas
  • Import CSV into MongoDB using python