pakallis / python-pandas-mongo

Painlessly integrate pandas dataframes with MongoDB
MIT License
28 stars 2 forks source link

How to connect with MongoDB Atlas? #3

Open mannerism opened 1 year ago

mannerism commented 1 year ago

Hello,

First of all, great work you've done to provide this tool. Is there a way for me to connect pandas with mongodb atlas URI?

pakallis commented 1 year ago

Hi @mannerism!

Thanks!

To connect to MongoDB Atlas you can use something like that:

import pymongo
import pdmongo as pdm
import pandas as pd
from urllib.parse import quote_plus
from pymongo import MongoClient

url = f"mongodb+srv://<username>:{quote_plus('<password>')}@{'<cluster>?retryWrites=true&w=majority'}"

client = MongoClient(url)
df = pd.DataFrame({"A": [1,2]})
df.to_mongo("TestCollection", client['db'])
ddf = pdm.read_mongo("TestCollection", [], client['db'])

Where and are your MongoDB Atlas username and password and is the MongoDB Atlas cluster you want to connect to.

You can retrieve those details by clicking "Connect" in MongoDB atlas web UI.

Note

Make sure you have updated to the latest version of this library as I have fixed some issues.