-
-
Save mieitza/5c17179013a75c0b264b699f8a3a1801 to your computer and use it in GitHub Desktop.
Functions to connect and read mongodb data to pandas df
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pymongo import MongoClient | |
# set connection with mongodb | |
def _connect_mongo(host, port, username, password, db): | |
""" A util for making a connection to mongo """ | |
if username and password: | |
mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db) | |
conn = MongoClient(mongo_uri) | |
else: | |
conn = MongoClient(host, port) | |
return conn[db] | |
# read mongodb collection and move to pandas dataframe | |
def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True): | |
""" Read from Mongo and Store into DataFrame """ | |
# Connect to MongoDB | |
db = _connect_mongo(host=host, port=port, username=username, password=password, db=db) | |
# Make a query to the specific DB and Collection | |
cursor = db[collection].find(query) | |
# Expand the cursor and construct the DataFrame | |
df = pd.DataFrame(list(cursor)) | |
# Delete the _id | |
if no_id: | |
del df['_id'] | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment