How to Check If a Specific Database Exists By Name in Python


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.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments