MongoDB mongosh Create Database
MongoDB mongosh – Create Database 🗄️
👉 You don’t explicitly create a database — it is created automatically when you insert data into it.
Step 1️⃣ Open mongosh
Open terminal / command prompt and run:
If connected successfully, you’ll see:
Step 2️⃣ Check Existing Databases
📌 The database you create will not appear here until it contains data.
Step 3️⃣ Create (Switch to) a New Database
Use the use command:
Output:
⚠️ At this stage, the database is not yet created physically.
Step 4️⃣ Insert Data (This Creates the Database)
Insert a document into a collection:
Output:
✅ Now the database is created
Step 5️⃣ Verify Database Creation
You’ll now see:
Step 6️⃣ View Collections
Output:
Step 7️⃣ View Data
Important Notes ⚠️
-
MongoDB creates databases only after data insertion
-
Empty databases are not stored
-
Database names are case-sensitive
-
Avoid special characters in database names
