I want to analyze a database hosted in Mongo. Therefore, I want to connect the mongo URI to pandas, so I can run all my python queries freely in my Jupyter Lab environment.
Let's day that this is my mongo connection:
"mongoURI": "mongodb+srv://test:test12345@cluster0-ze0tw.mongodb.net/databasetest?retryWrites=true&w=majority"
I have read other answer, and they shared this code that supposedly might help me out with this connection. I am not quite if I am filling out the fields correctly. Can you guys walk me through using as an example the connection shown above.
import pandas as pd
from pymongo import MongoClient
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]
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
Seems like you are connecting to the MongoDB Atlas instance. First of all check if your Python client machine's IP is in the Atlas IP Whitelist (Security->Network Access->IP Whitelist).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.