• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read

You can create a Database in MongoDB by using use databasename and create a c collection using db.createCollection(). In MongoDB, a database is a container for collections, which in turn holds individual documents. In this article, we will see the creation of a MongoDB database via MongoDB clients like CLI and GUI.

Advertisements

1. Create a Database with MongoDB CLI

To create the database in MongoDB CLI, we need to open the mongosh shell and use the command use database_name, change the database according to your need.


# Syntax
use database_name

In MongoDB, the test database is the default database. Instead of the create command, MongoDB offers the use option to create the database. The use command will switch to an existing database or create a new one. Below, we have specified the database name, StudentDatabase, along with the use option.


# Creating database
use StudenDatabase

The output of creating the database in the mongosh shell indicates that we have switched to a database which is create by the same statement.

Create database in MongoDB

In order to verify which database we are currently connected to, we can execute the db command.


# Currently connected database
db

The execution of the db command gives the output like this, which generates the StudentDatabase we are connected with.

mongodb create collection

As we have switched to the desired database, now we can start creating the collections for adding the documents. Note that MongoDB only creates the database when we create the collection into it.

2. MongoDB Create Collection from CLI

You can create the collection in MongoDB by using the createCollection() method. The collection object in MongoDB stores the documents.


# Creating Collection
db.createCollection(“student”)

The results are obtained below after executing the command for creating the collection in the database.

create collection in MongoDB

3. Show MongoDB Databases

As we have created the database and successfully added the collection to that database. Let’s list all the databases in the MongoDB by using the show databases command.


# Check existing databases
show databases

This lists all the databases with the storage size that exists in MongoDB. Here, admin, config, and local are the default databases in MongoDB.

Create database in MongoDB

4. Create a Database with MongoDB GUI

In the above section, we have learned how to create the database using the mongosh CLI of MongoDB. Alternatively, we can use the GUI to create the database.

Here, we are using the MongoDB compass application for creating the database. All we need to do is just open MongoDB Compass, and after establishing the connection with MongoDB, just click on the Create Database button in the Databases tab like below.

Create database in MongoDB

The dialogue box will appear as shown on the following screen. Give the new database a name in the Database Name area of the Create Database dialogue box and give the database a new Collection Name as well. Then, click on the Create Database button.

create collection in mongodb

In the Databases tab of MongoDB Compass, we can find the newly created database listed.

Create database in MongoDB

We can then create collections within the database by clicking on the database name and clicking on the Add Collection button. However, when we established the database, the collection was already in place. This is useful if we want to add multiple collections to the same MongoDB database.

Create database in MongoDB

This is how the collection is listed in the database we have created. Now to insert data into the collection, click on the listed collection.

The above step will redirect to the following page, where we can insert documents and query documents.

Create collection in MongoDB

3. Conclusion

In conclusion, you can create a new database using the use command in MongoDB. The syntax for creating a new database in MongoDB is use <databasename>. Note that the use command will switch to an existing database or create a new one. You can create the collection in MongoDB by using the createCollection() method. The collection object in MongoDB stores the documents.