Pymongo MongoClient: if you put a database in via URI, how do you get it back out?
mongodb, python
Solution
I believe what you are looking for can be found in `pymongo.uri_parser`. In particular, look at the parse_uri function. `parse_uri` takes a MongoDB URI as an argument and returns a dictionary containing values such as `username`, `password` and, most importantly, `database`.
Example:
from pymongo.uri_parser import parse_uri
mongo_uri = 'mongodb://james:brewer@localhost/test'
for k, v in parse_uri(mongo_uri).items():
print k, ':', v
will print
username : james
nodelist : [('localhost', 27017)]
database : test
connection : None
password : brewer
options : {}
Hope this helps!
Problem
The documentation for `MongoClient` says that one may say 'host=' and give a full MongoDB URI. Those include a database name. Once I do that, is there some way to extract the db name from the MongoClient object? I'm failing to spot it in the source code.