Import CSV File into MongoDB using Python
In this tutorial how to explain Import CSV File into MongoDB using Python.While developing applications using Python, from time to time we need to insert CSV data into the MongoDB database. Inserting CSV data to MongoDB is very easy in Python. We just need to read the CSV file and then connect to MongoDB to insert data. So we will use module
pymongo
to connection with the MongoDB client and insert data.Step 1 – Install PyMongo Module
Firstly, we need to run connect with the MongoDB client, so we need to install
pymongo
module using this command.
1 2 |
//run command pip install pymongo |
Step 2 – Connect to MongoDB
We will import module
pymongo
to connect with MongoDB to insert records in database.
1 |
from pymongo import MongoClient |
We will pass MongoDB connection details and connection to the database and collection to insert records.
1 2 3 |
mongoClient = MongoClient() db = mongoClient.october_mug_talk db.segment.drop() |
Step 3 – Reading CSV File
We will insert CSV file data to MongoDB, so first, we will read CSV files and convert data into JSON. Import CSV module at the top of the file to read CSV file.
1 |
import csv |
We will CSV file name user using
DictReader()
method. The DictReader()
function returns a csv reader object.
1 2 |
filename = open('user.csv', 'r') reader = csv.DictReader(filename) |
We will iterate(loop) over the CSV reader object and create JSON data to insert multiple records into the MongoDB database.
1 2 3 4 |
for each in reader: rows={} for field in header: rows[field]=each[field] |
Step 4 – Inert Data into MongoDB
Finally, we will insert JSON rows data into MongoDB using
insert_many()
method.
1 |
db.segment.insert(rows) |
Step 5 – Insert CSV data into MongoDB
Final code to import CSV data into MongoDB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// import code import csv from pymongo import MongoClient mongoClient = MongoClient() db = mongoClient.october_mug_talk db.segment.drop() header = [ "name", "email", "address"] filename = open('user.csv', 'r') reader = csv.DictReader( filename ) for each in reader: rows={} for field in header: rows[field]=each[field] print (rows) db.segment.insert(rows) |