MongoDB mongosh Create Database

If you are interested to learn about the MongoDB Get Started

Create Database using mongosh

After connecting to your database using mongosh, you can see which database you are using by typing db in your terminal.

Show all databases

To see all available databases, in your terminal type show dbs. Notice that myFirstDatabase is not listed. This is because the database is empty. An empty database is essentially non-existant.

Change or Create a Database

You can change or create a new database by typing use then the name of the database.

Example

Create a new database called “blog”:

use blog

Create a New Database : You can create a new Database in MongoDB by using “use Database_Name” command. The command creates a new database if it doesn’t exist, otherwise, it will return the existing database.you can run this command in mongo shell to create a new database. Your newly created database is not present in the list of Database. To display database, you need to insert at least one document into it.

Syntax:

 use Database_Name

Example: CREATING A NEW DATABASE

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 : You can check currently selected database, using the command “show dbs“. Your newly created database is not present in the list of Database. To display any database, you need to insert at least one or more document into it.

Example: LIST OF DATABASES

Check current Database : You can check list of databases, using the command “db

Example: CHECKING CURRENTLY SELECTED DATABASE

Switch to other Database : You can switch to other database using the command “use Database_Name“. If Database does not exists then it will create a new Database.

Example: SWITCHING TO ANOTHER DATABASE

MongoDB mongosh Create Database
Show Buttons
Hide Buttons