S Andrew
S Andrew

Reputation: 7298

Unable to get the collection names from MongoDB python

I have mongodb running on azure portal. I can connect to it using nosql booster. I have created a DB TestDb and have added 3 collections to it. I am trying to connect to it using python as below:

mongo_url = 'mongodb://' + <username> + ':' + <password> + '@' + <url> + ':' + port + '/' + admin
client = MongoClient(mongo_url)
db = client.get_database('TestDb')
print(db)
print(db.list_collection_names()) # Error at this line

Below is the output of db:

Database(MongoClient(host=['<name>.documents.azure.com:10255'], document_class=dict, tz_aware=False, connect=True), 'TestDb')

but at db.list_collection_names() it shows error <name>.documents.azure.com:10255: timed out.

I have rechecked everything and all looks good to me. But not sure why not able to do above using python. Please help. Thanks

Upvotes: 0

Views: 1031

Answers (2)

Jordy Cuan
Jordy Cuan

Reputation: 487

I was also using a test DB but nothing worked. This develop DB had some dummy configurations so the solution was adding a tlsAllowInvalidCertificates to my url:

url = f"mongodb://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DB_NAME}?authSource=admin&ssl=true&tlsAllowInvalidCertificates=true"

Upvotes: 0

Alex Blex
Alex Blex

Reputation: 37108

It's worth mentioning you are using Cosmos DB.

Although it's compatible with MongoDB on wire protocol level, it has own specifics.

Try to follow Quick Start snippets for Python from Azure Portal. It should have most accurate connection settings.

My best guess is it requires ssl enabled oclientside:

mongo_url = 'mongodb://' + <username> + ':' + <password> + '@' + <url> + ':' + port + '/' + admin + '?ssl=true'

Upvotes: 1

Related Questions