MongoDB mongosh Create Database
MongoDB mongosh Create Database
In MongoDB mongosh Create Database are created automatically when you store data in them.
There is no CREATE DATABASE command like in SQL.
1️⃣ Start mongosh
Open Terminal / Command Prompt and run:
You’ll see a prompt like:
2️⃣ Check Existing Databases
Example output:
3️⃣ Switch to (Create) a New Database
Use the use command:
Output:
Important:
At this stage, the database is NOT actually created yet.
4️⃣ Create Database by Inserting Data
MongoDB creates a database only when data is inserted.
Example: Create a collection and insert a document
Output:
Now the database is officially created.
5️⃣ Verify Database Creation
Now you’ll see:
6️⃣ Check Current Database
Output:
7️⃣ Create Multiple Collections (Optional)
MongoDB automatically creates:
-
productscollection -
orderscollection
8️⃣ Create Empty Collection (Optional)
If you want a collection without inserting data:
Output:
9️⃣ Delete a Database (Extra – Interview Tip)
Deletes the current database completely.
Key Points (Very Important for Exams & Interviews)
✔ MongoDB creates databases automatically
✔ use dbName → switches database context
✔ Database appears only after data insertion
✔ Collections are creating automatically
✔ No CREATE DATABASE command in MongoDB
Common Interview Questions
Q1. Does use myDB create a database?
👉 ❌ No, it only switches context
Q2. When is a database actually creating?
👉 ✅ When the first document is inserting
Q3. Can MongoDB have empty databases?
👉 ❌ No, it must contain data
