Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get list of all databases in a mongo instance using pymongo

how to get the list of all the databases in a mongo instance to a variable using pymongo?

for example to send following command to mongo instance using pymongo,

db.adminCommand( { listDatabases: 1 } )

like image 728
Siva Dasari Avatar asked Apr 19 '17 16:04

Siva Dasari


1 Answers

Use database_names

dbs = MongoClient().database_names()

As Andrew Allaire points out: Starting in pymongo 3.6 database_names() has been deprecated in favour of list_database_names.

dbs = MongoClient().list_database_names()
like image 55
Steve Rossiter Avatar answered Oct 08 '22 07:10

Steve Rossiter