How to create new database in MongoDB

In this article, we will learn “How to create a new database in MongoDB“. We will also understand how differently we can create a database in MongoDB, MongoDB Atlas, and compass. The topics that we are going to cover are given below.

  • Create a new database in MongoDB
  • How to create a new database in MongoDB using Atlas
  • How to create new database in MongoDB using compass

How to create a new database in MongoDB

MongoDB does not provide any special command for creating a database.

If you are from an SQL background, There you have to create a database, table and insert the values in the table manually.

But, In MongoDB, It will create automatically when you save the value into the defined collection at the beginning.

If you want to learn more about MongoDB, you can click here: What is MongoDB database

How to create a database

In MongoDB “use database_name” is a command that is used to create a new database. This command only creates a new database if it doesn’t exist.

Syntax:

use DATABASE_NAME

For example:

We are using database name as “mydatabase”:

>use mydatabase
switched to db mydatabase

If we want to check on which database we are currently working, we simply use the command “db” and this will show the name of the database in which we are working.

>db
mydatabase

After executing this command, if you want to check, Databases list you can use this command “show dbs“.

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

Remember, You create a database “mydatabase” that is not available in the list. If you want to display that this database, you need to insert at least one document into it.

> db.mydatabase.insert({"name":"sql server guides"})
> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mydatabase  0.000GB

Note that in MongoDB, if you don’t create any database then there is a default database called “test” then collections will be stored in this database.

Read: How to install MongoDB

How to create a new database in MongoDB using Atlas

What is atlas

MongoDB Atlas is a fully supervised cloud database used for modern applications that are developed by the same people who build MongoDB.

It’s an open-source cloud database that handles all of the complexities of deploying, administering, and assisting you with deployments on your preferred cloud service provider.

These are some main cloud service providers:

  • AWS (Amazon Web Services)
  • GCP (Google Cloud Platform)
  • Azure (Microsoft Azure)

Atlas is available in 70+ regions across AWS, GCP, Azure. With the help of all these cloud services, We can easily create the database.

How to create Databases and Collections in Atlas:

From the collections tab, Here you can see top-left there is a button for creating the database and on the top-right side, there is a green color button for creating a collection.

For creating a database follow these steps:

  • Click on create database button.
  • Now simply enter database name and collection name. Here, you can also create a capped collection.
  • Click on create button.

This will successfully create a database and you can see this on data explorer.

Note, If you want to know about the capped collection:

  1. It is a fixed-size collection.
  2. Its means when we create the collection(Bytes) then there we fix the maximum size of the collection so that much maximum number of document we can store.
  3. Its support high-throughput operations
  4. We can insert and retrieve documents based on insertion order.

For creating a collection follow these steps:

  1. Creating a database click on ‘+’ sign icon
  2. Now enter the collection name and also specify capped collection if you want to
  3. Click on create button

This is the simplest way for creating the collection.

Read: How to create a table in PostgreSQL

How to create new database in MongoDB using compass

What is the MongoDB compass?

MongoDB Compass is a Powerful GUI. It is basically used to explore data visually. Its visual environment helps us querying, aggregating, and analyzing MongoDB data.

Compass is free to use and we can easily run on macOS, Windows, and Linux.

How to create a database in MongoDB Compass

A Database is a container of collections, A single database we have multiple collections. In the collection, we store multiple documents.

For creating the database you need to follow the below steps:

  • Open MongoDB compass.
create a database in MongoDB compass
Compass
  • There is database tab, where you have to click on “Create Database” button. It will bring forword “Create database” dialog.

First, Write the database name and then the collection name. Both the names are required.

If you want to use the Capped collection, Custom collation, and time-Series select the checkbox as per your requirement.

Click on the “Create Database” button.

create database in compass
enter the name of the database and collection
  • This way we can easily create the database in MongoDB Compass.
MongoDB compass create db
successfully created database name
“database”

You may also like reading the following articles.

So in this tutorial, we have discussed How to create a new database. And we also covered the below topics with some practical implementation.

  • Create a new database in MongoDB
  • How to create a new database in MongoDB using Atlas
  • How to create new database in MongoDB using compass