Wednesday, April 5, 2023

Creating Database and Collection

The creation of any database and collection is a very simple process in MongoDB. You can use the syntax of retrieval to do this. If you try to access a database which doesn’t exist, MongoDB will create it for you.

Let’s create a database and a collection:

mydb=client.testDB

mycoll=mydb.testColl

The MongoDB database has been created here but if we run list_database_names, this database will not be listed. MongoDB doesn’t show empty databases. So, we will have to insert something there. Let’s insert a document in the MongoDB collection:

testInsert=mycoll.insert_one({"country":'India'}).inserted_id

client.list_database_names()


Now we can see that our database is available in the list of MongoDB databases.

Share:

0 comments:

Post a Comment