In this Python tutorial, I’ll guide you on importing CSV files into MongoDB using Python.
Sometimes, I 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.
We will utilize the built-in CSV
module to read CSV files and the pymongo
module to connect with the MongoDB client and insert data.
You can also checkout other python tutorials:
- Reading Excel Using Python Pandas
- Read and Write CSV Data Using Python
- Import CSV File into MongoDB using Python
- Read CSV File Using read_csv() method
- Read CSV file Using Numpy
Import CSV into Python
Let’s create a function in our Python app to import CSV into Python.
import csv
How to Open a CSV file in Python
To open and read a CSV file in Python, you can use the built-in csv
module. The csv
module provides functionality to handle CSV files easily. Here’s an example of how to open a CSV file and read its contents in Python:
import csv with open('file.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)
in the above code, We have opened the file named "file.csv"
, reads its content row by row, and print each row to the console. Remember to replace "file.csv"
with the actual path or filename of your CSV file.
To import a CSV file into MongoDB using Python
We’ll import a CSV file into MongoDB using Python. We will cover the necessary steps, and install dependencies including establishing a connection to MongoDB, reading the CSV file, and inserting the data into a MongoDB collection.
Install PyMongo Module
The PyMongo Module helps to connect with MongoDB client, so we need to install pymongo module using the below command.
pip install pymongo
How To Connect Python App With MongoDB
We will import pymongo module at the top of the app.py
file to connect with MongoDB.
from pymongo import MongoClient client = MongoClient('<mongo_connection_string>')
Let’s create a mongodb client using MongoClient()
method and pass the connection details. Replace <mongo_connection_string>
with your actual MongoDB connection string.
Selecting the Database and Collection
Next, specify the database and collection where you want to import the data. Use the client
object to access the desired database and collection.
db = client['your_database_name'] collection = db['your_collection_name']
Replace 'your_database_name'
and 'your_collection_name'
with the names of your database and collection, respectively.
Reading CSV File
We’ll be inserting data into MongoDB from a CSV file, So we’ll need to read the CSV file and convert the data to JSON first. To read a CSV file, we will import the CSV module at the top of the file.
import csv csvfile = open('employee.csv', 'r') reader = csv.DictReader( csvfile )
As you can see in the code above, I opened the employee.csv
file in reading mode before passing it to the DictReader()
function, which returns a CSV reader object.
To insert several records into MongoDB, we will iterate through the CSV reader object and create JSON data.
for each in reader: row={} for field in header: row[field]=each[field]
Insert Data into MongoDB
We’ll use the insert_row() method to insert JSON row data into MongoDB.
db.segment.insert(row)
Closing the Connection
Once you have finished importing the data, it is important to close the connection to MongoDB by using the close
method on the client
object:
client.close()
Full Source code to insert CSV data into MongoDB in Python
import csv from pymongo import MongoClient # Establish a connection to MongoDB client = MongoClient('<mongo_connection_string>') # Select the database and collection db = client['your_database_name'] collection = db['your_collection_name'] header = [ "name", "age", "country"] # Read and insert CSV data into MongoDB with open('employee.csv', 'r') as file: csv_data = csv.DictReader(file) for row in csv_data: collection.insert_one(row) # Close the connection client.close()
Conclusion:
This tutorial demonstrated how to import CSV data into MongoDB using Python. I have achieved this easily with the help of the pymongo
package. Once you have established a connection to MongoDB, after that you can read the CSV file, and insert the data into the desired collection.
Thank you for your extremely great info.
thanks for sharing this code, it solved my problem