In this example we will show how to check if a specific database exists by name in Python MongoDB.
Source Code
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
db_list = client.list_database_names()
if "mydatabase" in db_list:
print("The database exists.")
else:
print("The database doesn't exist.")
Output:
The database exists.