Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to list collections in azure document db with mongodb protocol support

I am currently using Python3 (pymongo) to connect to an Azure Document DB with Mongo protocol support.

# reference to connection string
self.connection_string = "mongodb://<user>:<pw>@<location>:<port>/<database>?ssl=true"

# creates the connection (this is working)
self.mongo_client = MongoClient( self.connection_string )

# show databases and there collections
print(self.mongo_client.database_names())
for db_name in self.mongo_client.database_names():
    print(db_name,">",self.mongo_client[db_name].collection_names())

Running the above snippet of code lists the databases however the collections are not listed. Running this on a local mongo db works as expected.

I was originally trying to run a query on a known collection within the database however, I can't even seem to be able to do that. I have MongoChef connected and is working as expected (able to run querys).

Any ideas what I could be doing wrong?

like image 903
Greener Avatar asked Nov 21 '25 09:11

Greener


1 Answers

@Greener, the issue seems to be caused by pymongo, not DocumentDB with MongoDB protocol.

I tried to connect DocumentDB with MongoDB protocol using the third party tool Robomongo successfully, and I could see the collections as below.

enter image description here

As reference, here is a workaround way for listing collections in PyMongo via Database level command listCollections, please see below.

>>> mongo_client.command('listCollections')
{'ok': 1, '_t': 'ListCollectionsResponse', 'cursor': {'firstBatch': [{'options': {}, 'name': 'testCollection'}], 'ns': 'testdb.$cmd.listCollections', 'id': 0}}

Hope it helps.

like image 86
Peter Pan Avatar answered Nov 24 '25 00:11

Peter Pan