Skip to main content

Import CSV File into MongoDB using Python

In our Python tutorial, you have learned how to read a CSV file in Python. In this tutorial, you will learn how to import CSV file into MongoDB using Python.

while developing application using Python, sometimes we need to insert CSV data into MongoDB database. Inserting CSV data to MongoDB is very easy in Python. We just need t read CSV file and then connect to MongoDB to insert data.

So here in this tutorial, we will use CSV built-in module to read CSV file. We will use module pymongo to connection with MongoDB client and insert data.

So let’s start coding:

Install PyMongo Module

As we need to connect with with MongoDB client, so w need to install pymongo module using below command.

pip install pymongo

Connect to MongoDB

We will import module pymongo to connect with MongoDB to insert records.

from pymongo import MongoClient

then we will pass MongoDB connection details and connection to the dababase and cllection to insert records.

mongoClient = MongoClient()
db = mongoClient['emp_data']
collection = db['emp']

Reading CSV File

As we will insert CSV file data to the MongoDB, so first we will read CSV file and convert data into JSON. So we will import csv module at the top of file to read CSV file.

import csv

then we will CSV file employee using DictReader() method. The DictReader() function returns a csv reader object.

csvfile = open('employee.csv', 'r')
reader = csv.DictReader( csvfile )

we will iterate over csv reader object and create json data to insert mulitple records into MongoDB.

for each in reader:
    row={}
    for field in header:
        row[field]=each[field]

Inert Data into MongoDB

Finally, we will insert json row data into MongoDB using insert() method.

collection.insert(row)

Complete code to insert CSV data into MongoDB

Here is complete code to import CSV data into MongoDB.

import csv 
from pymongo import MongoClient

mongoClient = MongoClient()
db = mongoClient['emp_data']
collection = db['emp']

header = ['name', 'age', 'country']
csvFile = open('employee.csv', 'r')
reader = csv.DictReader(csvFile)

for each in reader:
    row = {}
    for field in header:
        row[field] = each[field]
    collection.insert(row)