how to issue a "show dbs" from pymongo


Question

I'm using pymongo and I can't figure out how to execute the mongodb interactive shell equivalent of "show dbs".

1
59
6/22/2012 7:02:43 PM

Accepted Answer

from pymongo import MongoClient
# Assuming youre running mongod on 'localhost' with port 27017
c = MongoClient('localhost',27017)
c.database_names()
92
2/7/2014 8:24:41 AM

as today it's

from pymongo import MongoClient
# client = MongoClient('host', port_number)
client = MongoClient('localhost', 27017)
cursor = client.list_databases()
for db in cursor:
    print(db)

or

from pymongo import MongoClient
# client = MongoClient('host', port_number)
client = MongoClient('localhost', 27017)
for db in client.list_databases():
    print(db)

If you use database_names, you will get "DeprecationWarning: database_names is deprecated. Use list_database_names instead."


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon