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

Asked By: jacobra

||

Answers:

from pymongo import MongoClient
# Assuming youre running mongod on 'localhost' with port 27017
c = MongoClient('localhost',27017)
c.database_names()

Update 2020:

DeprecationWarning: database_names is deprecated

Use the following:

c.list_database_names()
Answered By: jacobra

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

Answered By: Shailyn Ortiz

With Python3.5, you can try this way

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
print(client.list_database_names())
Answered By: cyrilsebastian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.