Are you want to connect your MongoDB database using python program? If your answer is Yes, then this tutorial is for you. In this tutorial, we are going to share the MongoDB database connection in python with example.
MongoDB will create the database if it does not exist, and make a connection to it.
myclient = pymongo.MongoClient("mongodb://localhost:8989/")
mydb = myclient["mydatabase"]
Use the below code to validate is database already exist.
dblist = myclient.list_database_names()
if "mydatabase" in dblist:
print("The database exists.")
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.