how to issue a "show dbs" from pymongo

PythonMongodbPymongo

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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()

Solution 2 - Python

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

Solution 3 - Python

With Python3.5, you can try this way

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
print(client.list_database_names())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjacobraView Question on Stackoverflow
Solution 1 - PythonjacobraView Answer on Stackoverflow
Solution 2 - PythonShailyn OrtizView Answer on Stackoverflow
Solution 3 - PythoncyrilsebastianView Answer on Stackoverflow