0

I have written the below Python program to create a MongoDB.

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['mydatabase']

dblist = myclient.list_database_names()

if "mydatabase" in dblist:
  print("The database exists.")
else :
  print("The database does not exists.")

But while I execute the program , I get the result as

The database does not exists.

Why so? Is there something that I am missing ?

I am following the code mentioned in W3Schools Tutorial

2
  • I know it's stupid but... do you have mongodb running on localhost at port 27017? Commented Nov 19, 2019 at 16:31
  • yes ofcourse... i am following the guidance in the tutorial because I am new to Mongo, Python ..... and in 1 window - Mongo is running while in the other i am executing the python script (: Commented Nov 19, 2019 at 16:34

2 Answers 2

1

Databases are lazily created. Insert something into a collection in that database and they (database/collection) will spring into existence.

Sign up to request clarification or add additional context in comments.

Comments

1

In MongoDB the database is not actually created until you put some data into it, try put a document inside and check again.

Important: In MongoDB, a database is not created until it gets content!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.