Chapter 4: MongoDB mongosh Create Database
What is MongoDB mongosh Create Database?
This is super common confusion for beginners, so let’s clear it 100% like a patient teacher sitting next to you with a notebook.
Big Truth First (Write this down!)
MongoDB does NOT have a direct “CREATE DATABASE” command like MySQL (CREATE DATABASE shop;) or PostgreSQL.
Instead, MongoDB follows a “lazy creation” philosophy:
A database (and even a collection) is automatically created the first time you store data in it.
You don’t create an empty database upfront — MongoDB waits until you actually put something inside.
And the main way you “tell” MongoDB which database you want to work with (and create it if missing) is the use <database_name> command inside mongosh.
So when people say “mongosh Create Database“, what they really mean is:
How to create / switch to a database using mongosh (the modern MongoDB shell)
Step-by-Step Explanation – Like We’re Doing It Together
-
Connect to mongosh first (Assuming you already have Atlas or local MongoDB running — from our Getting Started class)
Bash0123456mongosh "mongodb+srv://youruser:yourpassword@cluster0.abcde.mongodb.net/"You land in the default/test database or whatever your connection string specifies.
-
Check current database
Type:
JavaScript0123456dbOutput example: test (default when nothing is selected)
-
See existing databases
JavaScript0123456show dbsYou might see:
text012345678admin 0.000GBconfig 0.000GBlocal 0.000GBYour new one won’t appear yet — because it’s empty!
-
The Magic Command: use
JavaScript0123456use myHyderabadShopOutput:
text0123456switched to db myHyderabadShop- If myHyderabadShop already existed → it just switches to it.
- If it did NOT exist → MongoDB prepares it (but doesn’t physically create files yet).
At this moment — still no database is visible in show dbs!
-
Actually Create It (by inserting data)
This is the key step most tutorials emphasize:
JavaScript01234567891011db.products.insertOne({name: "Hyderabadi Dum Biryani",price: 350,spicyLevel: "Medium",available: true})Output something like:
text0123456789{acknowledged: true,insertedId: ObjectId('67ab1234cdef567890abcdef')}NOW — magic happens!
Run again:
JavaScript0123456show dbsNow you see:
text0123456789admin 0.000GBconfig 0.000GBlocal 0.000GBmyHyderabadShop 0.000GB ← appeared!→ Database physically created only after first document was inserted.
-
Alternative ways people create database + collection in one go
JavaScript01234567891011use blogAppdb.posts.insertMany([{ title: "Valentine Special Biryani", content: "Love is spicy!", likes: 42 },{ title: "MongoDB Tips", content: "use + insert = create", likes: 15 }])Same effect — database blogApp + collection posts created instantly.
Quick Comparison Table – MySQL vs MongoDB (Very Helpful!)
| Action | MySQL / SQL | MongoDB (mongosh) |
|---|---|---|
| Create empty database | CREATE DATABASE shop; | No direct command |
| Switch / prepare database | USE shop; | use shop; |
| When is DB actually created? | Immediately on CREATE | Only when you insert first document |
| Command to list databases | SHOW DATABASES; | show dbs |
| Create table/collection + data | CREATE TABLE … then INSERT | Just db.items.insertOne({…}) |
Important Notes (Don’t Miss These!)
- Database names are case-sensitive in most cases (but avoid weird characters).
- You cannot create empty databases that stay forever — if you use newDb but never insert anything → it disappears after restart / won’t show in show dbs.
- In MongoDB Atlas UI → you can create database + collection visually (Create Database button) → that inserts a dummy doc behind the scenes.
- In code (Node.js / Python) → same rule: first insertOne / insertMany creates db + collection.
Mini Exercise – Do This Right Now!
- Open mongosh
- show dbs
- use class2026Feb14
- db.students.insertOne({ name: “Rahul”, city: “Hyderabad”, mood: “Learning MongoDB ❤️” })
- show dbs → see your new database!
- db → confirm you’re in class2026Feb14
- db.students.find().pretty()
Done! You just created your first database the MongoDB way.
Understood beta? 😄
This is one of those “aha!” moments in MongoDB — once it clicks, you never forget.
Next class — what do you want?
- How to create collection explicitly (with options like validator, capped)?
- Drop database / drop collection safely?
- List all collections in current db?
- Work with multiple databases in one session?
- Or jump to real project — like a simple student management system?
Tell me — class is continuing! 🚀
