Introduction
Learn how to create a MongoDB database using the mongosh
shell interface.
Connecting to MongoDB
Before creating a database, ensure you are connected to your MongoDB server:
Example: Connecting to MongoDB
mongosh
This command starts the mongosh
shell and connects to the default MongoDB server running locally.
Creating a Database
To create a new database in MongoDB, use the use
command:
Example: Creating a Database
use mydatabase
This command creates a new database named “mydatabase”. If the database already exists, it switches to that database.
Verifying Database Creation
Verify that the database has been created by listing all databases:
Example: Listing Databases
show dbs
This command lists all databases on the MongoDB server. Your newly created database should appear in the list.
Conclusion
Creating a MongoDB database using mongosh
is a straightforward process. After creating the database, you can start adding collections and documents to store your data.