In this post, we will learn the process of MongoDB Database Creation using the Mongo shell commands.

We have used MongoDB in our posts about Spring Data MongoDB and NestJS MongoDB integration. However, this will also be the beginning of a new series where we will learn MongoDB in depth. This post will help us set the groundwork for more complicated aspects about MongoDB.

1 – Database, Collections and Documents

A database is top most hierarchy in a MongoDB server. Normally, you would like to create a MongoDB database on an application level or tenant level in case you are going for multi-tenancy.

Within a database we have collections. You can think of collections as equivalent to tables from the SQL world. Going further down, a collection comprises of one or more documents. Here, documents are basically records. However, in the NoSQL terminology, we refer to them as documents.

Below illustration shows the hierarchy:

mongo database creation hiearchy

2 – MongoDB Database Creation

In order to create a new MongoDB Database, we can use GUI tools. However, it is much simpler to do so using the Mongo shell as well.

Once we open the shell, we can first issue the below command:

> show dbs

If this is the first time we are using the server, we will see a list of default databases.

admin   0.000GB
config  0.000GB
local   0.000GB

These are databases that come along with the MongoDB installation and are used mostly for configuration purpose.

However, we want to create a new database. To do so, we can execute the below command:

> use library
switched to db library

Now, this will not actually create the database. However, it will set the context of the future commands. In other words, the next command will execute on the library database. If the database does not exist and we try to perform any operation upon it, MongoDB will implicitly create the database for us.

3 – Insert Record to MongoDB Database

The first operation we can perform is to insert one record to our database.

As discussed earlier, we store records (or documents) in collections. Let us assume that we want to have a books collection in our library database.

Below is how we can insert a record into the same.

> db.books.insertOne({"name":"The Way of Kings","author":"Brandon Sanderson"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("61caa564f04e7275ecf04c4c")
}

Here, the keyword db stands for the current database context. In our case, it is the library database. Next, we point to the books collection. If such a collection is not present, MongoDB will implicitly create it for us. Then, we use the command insertOne() to insert a document.

In MongoDB, documents are structure as JSON objects. As you might know, JSON comprises of key/value pairs. Our JSON schema has two fields.

{
	"name": "The Way of Kings",
	"author": "Brandon Sanderson"
}

The command returns an acknowledgment and also an insertedId. In MongoDB, every document receives a unique ObjectId or the insertedId. This indicates that the insertOne executed successfully.

If we execute the command to show existing databases, we will also see the library database. See below:

> show dbs
admin    0.000GB
config   0.000GB
library  0.000GB
local    0.000GB

Basically, the database was created by MongoDB. Also, if we execute a command to show the collections, we will see our new collection as well.

> show collections
books

Conclusion

With this, we have successfully learnt the process of MongoDB Database Creation. We also created a new collection and inserted documents into the same.

In the next post, we will look into MongoDB CRUD Operations.

If you have any comments or queries, please mention in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *