3

I try to create database with simple Python code and It does not work.

When I try to create database, I cant see my database name in python and MongoDb.

Also, When I create database in MongoDb I can see my database name in Python. My Python version is 3.6

Can you help me please ?

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")

mydb = client["mydatabase"]

print(client.list_database_names())
3
  • What is the error that you get when running this code? Commented Aug 29, 2019 at 17:44
  • Actually, I did not get error, I can not see my database name when I try to list database names Commented Aug 29, 2019 at 17:51
  • Do you see your other database names, just not "mydatabase"? Commented Aug 29, 2019 at 17:59

1 Answer 1

6

The new database isn't created until you do your first insert.

Try adding an insert like below (also creating a collection) and you should see your new db.

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")

mydb = client["mydatabase"]  # you can also use dot notation client.mydatabase
mydb.mycoll.insert_one({"test": 'test'})
print(client.list_database_names())

The database mydatabase is output in the list of database names.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.