Welcome to TheCodingCollege.com, your trusted source for learning coding and programming! In this guide, we’ll walk you through the process of creating a database in MongoDB using mongosh, MongoDB’s interactive shell. If you’re new to MongoDB, this post will help you get started with practical examples and expert tips.
What is mongosh?
mongosh (MongoDB Shell) is an interactive command-line tool for connecting to and interacting with MongoDB. It allows developers to perform database operations, run queries, and manage data directly from the terminal.
Step 1: Install mongosh
Before you can create a database using mongosh, ensure it’s installed on your system. If you haven’t installed it yet, follow these steps:
- Download MongoDB Community Edition: Download here.
- Install mongosh: It is included with the MongoDB installation or can be installed separately via MongoDB Shell download.
Verify Installation
Open your terminal and type:
mongosh --version
You should see the installed version of mongosh.
Step 2: Connect to MongoDB
To start using mongosh, connect to your local or cloud-hosted MongoDB instance.
- Start the MongoDB server:
mongod
- Launch mongosh:
mongosh
- Connect to a MongoDB Instance (Local or Cloud):
- Local:
mongosh
- MongoDB Atlas (Cloud): Use the connection string provided by Atlas:
mongosh "your_connection_string"
Step 3: Creating a Database in mongosh
MongoDB doesn’t explicitly create a database until you insert data into it. However, you can switch to a new database context and start working with it immediately.
Steps to Create a Database:
- Switch to a New Database: Use the
use
command to create or switch to a database:
use myNewDatabase
- Output:
switched to db myNewDatabase
- Verify the Database: Run:
db
This returns the name of the current database. - List All Databases: Newly created databases won’t appear in the list until data is added. To see the list of databases:
show dbs
Step 4: Adding Data to Activate the Database
MongoDB activates a database only when it contains at least one collection with data.
Example: Adding a Document to Create a Collection
- Create a collection and insert a document:
db.myCollection.insertOne({ name: "Alice", age: 25, role: "Developer" })
- Output:
{
acknowledged: true,
insertedId: ObjectId("64d1f3a12c5a4c1e8bcd1234")
}
- Verify the Database Exists:
show dbs
- Output:
admin 0.000GB
config 0.000GB
local 0.000GB
myNewDatabase 0.001GB
Step 5: Deleting a Database (Optional)
If you need to delete a database, use the db.dropDatabase()
method while connected to it.
- Switch to the Database:
use myNewDatabase
- Drop the Database:
db.dropDatabase()
- Output:
{ ok: 1 }
Best Practices When Creating MongoDB Databases
- Use Meaningful Names: Choose database names that reflect their purpose, such as
ecommerceDB
oruserManagement
. - Enable Authentication: Secure your MongoDB instance by setting up user authentication.
- Backup Regularly: Use tools like
mongodump
to back up your databases. - Monitor Performance: Use MongoDB Atlas or other monitoring tools for insights into database health.
Final Thoughts
Creating a database using mongosh is a straightforward process that gives developers hands-on control over their MongoDB environment. With this guide, you’ve learned how to create, manage, and verify databases efficiently. MongoDB’s flexibility and simplicity make it a powerful tool for modern applications.