How to Create a Database in MongoDB

Creating a Database is the first step, where all the specific information is stored, so it is necessary to know how to create a database in MongoDB.

This beginner-level tutorial shows you how to create a database in MongoDB using two methods. As a database developer, you will usually use both the command line and the MongoDB Compass to manage data.

What is a Database in MongoDB?

A database is a collection of data organised in a structure; it is also a store collection of tables, each made up of rows and columns.

But these tables and row things are limited to SQL or another relational database like MySQL, PostgreSQL, etc.

In MongoDB, there is a database, but it stores a collection of documents instead of tables. So your data is stored in the collection of documents. Here, documents are like JSON data, which means your data is stored in JSON format.

Think of the database as a container that contains collections.

There are two ways to create a database in MongoDB: using the MongoDB compass or the MongoDB shell; I will explain both approaches here.

How to Create a Database in MongoDB Compass

To create a database in MongoDB Compass, first, make sure you have installed Compass on your system; if not, then follow this tutorial: How To Install MongoDB Compass

After installing the MongoDB compass, open it. On the right side, you will see the plus “+” icon in front of the Database tab. You need to click on this icon to create a database. Look at the picture below.

Create a Database in MongoDB Compass - Database Tab

After clicking on the “+” icon, a window appears showing Create Database, where you enter the database name in the field Database Name, and the collection name in the field Collection Name.

For example, I have entered the database name as “ecommerce” and the collection name as “user”. After specifying the name, click on the Create Database button at the bottom right corner, as shown below.

Create a Database in MongoDB Compass - Creating Database

Then, again, look at the right sidebar; you will see the created database named “ecommerce”, and under that database, the created collection is named “users”. Look at the below picture

Create a Database in MongoDB Compass - Viewing Database

From the above picture, you can see the database and collection that you have created. Now, you can create a document in the collection “users” to store the data.

Create a Database in MongoDB Shell

To create a database in MongoDB Shell, make sure you have installed MongoDB Shell on your system.

Open the MongoDB shell on your computer by typing the command below.

mongosh

After that, view all the databases using the command below.

show dbs

To create a database in MongoDB shell, you need to use the below syntax.

use database_name;

Here, use is the keyword followed by the database_name. If the specified database does not exist, it creates a new one.

Create a new database named “ecommerce” as shown below.

use ecommerce
Create a Database in MongoDB Shell

From the output, you can see the message “switched to db ecommerce,” which indicates that the database “ecommerce” has been created.

Now, using the command below, you can create a collection named “users”.

db.createCollection('users');

To view the create collection, use the command below.

show collections
Create a Database in MongoDB Shell - Creating Collection

As you can see, the new collection “users” is created using the “db.createCollection(‘users’)” command, and then it is viewed in the current database using the “show collections” command.

Conclusion

As a Database developer, this is the beginner step where you must know about a database and what it is, as well as how to create it. So you learned how to create a database using the MongoDB compass and shell, and you also learned how to create collections in a database.

You may like to read:

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.