I'm using pymongo and I can't figure out how to execute the mongodb interactive shell equivalent of "show dbs".
To get all the Documents of the Collection use find() method. The find() method takes a query object as a parameter if we want to find all documents then pass none in the find() method.
This SHOW DBS command won't show the databases because you may have not created a document for a collection. If you will create document for a collection then the created database will be visible.
In MongoDB, you can use the show dbs command to list all databases on a MongoDB server. This will show you the database name, as well as the size of the database in gigabytes. Example: > show dbs. Output.
Use the drop_database() method to delete a MongoDB database with PyMongo. NOTE: This API call does not return a results object to verify that the call was successful. If you try assigning a variable to the method call, it will just return a NoneType object.
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()
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With