Open In App

MongoDB – Create Database using Mongo Shell

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A MongoDB Database is the container for all the collections, where the Collection is a bunch of MongoDB documents similar to tables in RDBMS and the Document is made up of fields similar to a tuple in RDBMS, but it has a dynamic schema here.

This article explains the easy method to create a Database in MongoDB using Mongo Shell.

How to Create a MongoDB Database Using Mongo Shell

To create a new MongoDB database using Mongo Shell use the “use Database_Name” command. This command creates a new database if it doesn’t exist, otherwise, it will return the existing database.

The newly created database will not be present in the list of databases. To display the database in the database list, insert at least one document into it.

Syntax

 use Database_Name

Create a New Database Using Mongo Shell Example

The below image shows new database creation using Mongo Shell:

In MongoDB default database is test. If you did not create any Database and started inserting collection then all collections are stored in the Default Database.

Show list of Databases

To check currently selected database, use the command “show dbs“.

Show List of Databases Example

The below example shows how to see list of databases using Mongo Shell.

show list of databases example

Check Current Database

To check the current database use the “db” command.

Check Current Database Example

The below image shows how to check current database using Mongo Shell.

check current database example

Switch to Other Database

To switch to other database use the command ” use Database_Name” command. If Database does not exists then it will create a new Database.

Switch to other Database Example

The below image shows how to switch to another database using Mongo Shell.

switch to other database example

In the above Example, First we check current Database name using db command which was UserDB then we use “use test” command to switch to database test.

References: https://docs.mongodb.com/manual/core/databases-and-collections/#databases

Conclusion

This guide explained easy way to create a database using Mongo Shell. We also covered different database related operations using Mongo Shell. We discussed commands to view database list, check current database and switch to another database.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads