Script to list the names of all MongoDB databases using PyMongo.

from pymongo import MongoClient
#connect to local mongodb instance
client = MongoClient('localhost',27017)
#get the list of all MongoDB databases in the server
db_list = [db_name for db_name in client.database_names()]Code language: Python (python)

List non-default databases in MongoDB

If you do not want the default MongoDB databases (admin, local) to be included in the database names list, then modify the above script as follows.

from pymongo import MongoClient
#connect to local mongodb instance
client = MongoClient('localhost',27017)
#get the list of all MongoDB databases in the server except the default databases
db_list = [db_name for db_name in client.database_names() if db_name not in ('admin','local')]Code language: Python (python)

Download HitXP Mobile App

Get it on Google Play