3

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?

1
  • To the best of my knowledge, it looks like that the current version of the mongo protocol on document db does not support this and that is why it returns an empty list. Commented Nov 11, 2016 at 9:54

1 Answer 1

3

@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.