How To Create A MongoDB Database Step by Step

In this tutorial we will learn how to create a MongoDB database step by step with examples. After reading through the post you can easily practice it on your own.

First thing you need to keep in mind as a beginner just starting on MongoDB that it does not have a CREATEDATABSE that we use for SQL database creation. Rather it is much simpler than that, as we can easily create a MongoDB database switching to a non-existing database and then start adding datasets into it.

See the following example for a better understanding of creating a new Database in MongoDB:

use PetHotel

Result

switched to db PetHotel

Once we have done that, we will have a new created database with the name PetHotel. You can see the part of our new MongoDB has been created. We need to add datasets into it to complete it. So let’s say we want to run the dbs command to find it, it won’t show because we have not added any data. For instance

show dbs

Result

admin   0.000GB
 config  0.000GB
 local   0.000GB 

We have default databases that we installed with MongoDB. So if we created any other databases before this, they will all show here. So don’t panic when you don’t see the newly create database. We need to add some data for it show us.

Adding Data to New Database

To fully create the database, we need to insert some data. Even just a small amount is enough. The next we will add data to our database PetHotel:

db.pets.insert({ name: "Fetch" })

Result

 WriteResult({ "nInserted" : 1 })

The returned message shows we added one document to our database that will show now when we run dbs query again.

show dbs

Result

PetHotel  0.000GB
 admin     0.000GB
 config    0.000GB
 local     0.000GB 

Adding Collections to MongoDB Database

By now we have understood that database contains collections and we have created a collection in our database PetHotel called Pets. So for adding more data into our database we need to use the following syntax:

db.<collection>.insert()

So if we don’t have a collection it will create one. For our database that we have worked through a bit we used this command:

db.pets.insert({ name: "Fetch" })

Which created a collection called pets (because it didn’t already exist). It created a new collection in our database or if you have not already use that to create a collection called pets.

Let’s Query Again
We will use the find() collection method to find newly added data.

For instance:

db.pets.find()

Result

 { "_id" : ObjectId("5fe02d3937b49e0faf1af20b"), "name" : "Fetch" }

As expected, our document is returned.

Now we get the right results and we see an extra field with an _id field added which we did not add while adding the document data. The _id field is used to identify our document. Even if the document does not specify it, like we did not, still MongoDB adds _id field and adds a unique ObjectId to the document.

However, we can specify our own _id field with value.
Important Note: Please understand this, you can’t assign an _id field if it’s already been used. It has to be unique to avoid future errors while querying your database.