Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to issue a "show dbs" from pymongo

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

like image 644
jacobra Avatar asked Jun 22 '12 19:06

jacobra


People also ask

How do I get a document from PyMongo?

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.

Why show DBS does not show my databases in MongoDB?

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.

What is show DBS?

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.

How do I drop db PyMongo?

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.


2 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() 
like image 83
jacobra Avatar answered Sep 21 '22 02:09

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

like image 20
Shailyn Ortiz Avatar answered Sep 21 '22 02:09

Shailyn Ortiz