Interfacing with MongoDB from Python
Python can connect to MongoDB using the pymongo
module. To start off with, I will create a file called process_large_data.py
and import the pymongo
and csv
modules. You will need to import both pymongo
and MongoClient
as I have done in the demonstration:
import csv import pymongo from pymongo import MongoClient
MongoClient
takes care of establishing a connection and interfacing with the database system. The following steps in process_large_data.py
will create an object assigned to the collection variable, which can be used to insert documents into the database:
.... from pymongo import MongoClient ## create a MongoClient object, ## used to connect and interface ## with mongodb client = MongoClient() ## these two lines create a collection ## object which allows you to interface ## with a particular collection db = client.weather collection = db["records2"]
The only thing left to do now is to read the data line by line and insert each row as a record into...