Created
December 2, 2021 08:39
-
-
Save pessini/65038347c4ced46d21b5c8ebb2f87e4c to your computer and use it in GitHub Desktop.
Script to connect and query MongoDB using Python
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
# Database connections | |
# create a folder called conn/ and a python file called mongodb with variables for connection | |
from conn import mongodb | |
import importlib | |
from pymongo import MongoClient | |
from urllib.parse import quote_plus | |
importlib.reload(mongodb) | |
def _connect_mongo(host, port, username, password, db_name): | |
""" A util for making a connection to mongo """ | |
if username and password: | |
try: | |
mongo_uri = f'mongodb://{username}:{quote_plus(password)}@{host}:{port}/{db_name}' | |
conn = MongoClient(mongo_uri) | |
except: | |
print('Could not connect to MongoDB') | |
else: | |
conn = MongoClient(host, port) | |
return conn | |
def read_mongo(query={}, collection='', no_id=True): | |
""" Read from Mongo and Store into a DataFrame """ | |
df = None | |
try: | |
# Connect to MongoDB | |
conn = _connect_mongo(host=mongodb.host, port=mongodb.port, username=mongodb.user_name, password=mongodb.pass_word, db_name=mongodb.db_name) | |
db = conn.mobybikes # switch to the database | |
if collection in db.list_collection_names(): | |
# Make a query to the specific DB and Collection and store into a Dataframe | |
data = db[collection].find(query) | |
df = pd.DataFrame(list(data)) | |
# Delete the _id | |
if no_id: | |
del df['_id'] | |
else: | |
print(f'Collection {collection} was not found!') | |
pass | |
# close mongodb connection | |
conn.close() | |
except: | |
print('Could not query MongoDB') | |
return df | |
historical_data = read_mongo(collection='historical') // historical is the collection's name | |
# Example of querying all National holidays from a collection called irishcalendar | |
# If you let the parameter 'query' blank will select all data | |
qry_bh = { | |
'type': 'National holiday' | |
} | |
bank_holidays = read_mongo(query=qry_bh, collection='irishcalendar') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment