MongoDB is a popular NoSQL database known for its flexibility and scalability. Unlike traditional relational databases, MongoDB uses a document-based model and collections, making it ideal for modern applications. This blog provides a step-by-step guide to creating a database in MongoDB.

Step 1: Install MongoDB
Before you start, ensure MongoDB is installed on your system.
- Download MongoDB:
- Visit MongoDB’s official website.
- Choose the appropriate version for your operating system and install it.
- Start the MongoDB Server:
- Open your terminal or command prompt.
- Run the command:
mongod
- This will start the MongoDB server on your local machine.
Step 2: Open the MongoDB Shell
Once the server is running, open the MongoDB shell to interact with the database.
- Open a new terminal window or command prompt.
- Enter:
mongo
- This connects you to the running MongoDB server.
Step 3: Create a Database
In MongoDB, databases are created dynamically when data is inserted. However, you can specify a database explicitly to start working with it.
- Switch to a New or Existing Database:
- Use the
use
command:use myDatabase
- Replace
myDatabase
with your desired database name. - If the database doesn’t exist, MongoDB will create it once you insert data into it.
- Use the
- Verify the Database:
- After switching to the database, you can check the current database by typing:
db
- This will display the name of the active database.
- After switching to the database, you can check the current database by typing:
Step 4: Create a Collection
Collections in MongoDB are equivalent to tables in relational databases. To create a collection, insert data into it.
- Insert Data into a Collection:
- Use the
insertOne
method:db.myCollection.insertOne({ name: "John Doe", age: 30, city: "New York" })
- Replace
myCollection
with your collection name. - This command creates a new collection (
myCollection
) if it doesn’t exist and adds the provided document.
- Use the
- Verify the Collection:
- To check all collections in the database, run:
show collections
- To check all collections in the database, run:
Step 5: Insert and Query Data
Now that your database and collection are set up, you can insert and retrieve data.
- Insert Multiple Documents:
db.myCollection.insertMany([ { name: "Jane Doe", age: 25, city: "Los Angeles" }, { name: "Mike Smith", age: 35, city: "Chicago" } ])
- Query the Data:
- Retrieve all documents:
db.myCollection.find()
- Retrieve documents with specific criteria:
db.myCollection.find({ city: "New York" })
- Retrieve all documents:
Step 6: Confirm the Database Creation
MongoDB doesn’t permanently create a database until data is added. After inserting data, confirm the database creation:
- List All Databases:
- Run:
show dbs
- Your database (
myDatabase
) should now appear in the list.
- Run:
Step 7: Optional – Delete the Database
If you no longer need the database, you can delete it.
- Switch to the Database:
use myDatabase
- Drop the Database:
db.dropDatabase()
- This removes the database and all its collections permanently.
Helpful Resources
- Step-by-Step Guide to Create a Database on MongoDB Atlas
- 10 Most In-Demand Software Development Skills for 2025
