Create new database mongodb shell
Here are the steps to create a new database in MongoDB using the MongoDB shell:
Step 1: Connect to the MongoDB shell
Open a terminal or command prompt and type the following command to connect to the MongoDB shell:
mongo
This will start the MongoDB shell and connect you to the default database, which is usually called "test".
Step 2: Create a new database
To create a new database, use the following command:
use mydatabase
Replace "mydatabase" with the name of the database you want to create. If the database does not exist, MongoDB will create it.
Step 3: Verify the database creation
To verify that the database has been created, use the following command:
show dbs
This will list all the databases in your MongoDB instance, including the new database you just created.
Step 4: Use the new database
To use the new database, use the following command:
use mydatabase
This will switch the current database to the new one.
Example:
Here's an example of creating a new database called "mydatabase" and verifying its creation:
mongo
> use mydatabase
switched to db mydatabase
> show dbs
mydatabase test
In this example, we first connect to the MongoDB shell using the mongo
command. Then, we create a new database called "mydatabase" using the use
command. We verify that the database has been created by using the show dbs
command, which lists all the databases in our MongoDB instance. Finally, we switch to the new database using the use
command again.
That's it! You have now created a new database in MongoDB using the MongoDB shell.