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

  1. Connect to mongosh first (Assuming you already have Atlas or local MongoDB running — from our Getting Started class)

    Bash

    You land in the default/test database or whatever your connection string specifies.

  2. Check current database

    Type:

    JavaScript

    Output example: test (default when nothing is selected)

  3. See existing databases

    JavaScript

    You might see:

    text

    Your new one won’t appear yet — because it’s empty!

  4. The Magic Command: use

    JavaScript

    Output:

    text
    • 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!

  5. Actually Create It (by inserting data)

    This is the key step most tutorials emphasize:

    JavaScript

    Output something like:

    text

    NOW — magic happens!

    Run again:

    JavaScript

    Now you see:

    text

    → Database physically created only after first document was inserted.

  6. Alternative ways people create database + collection in one go

    JavaScript

    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!

  1. Open mongosh
  2. show dbs
  3. use class2026Feb14
  4. db.students.insertOne({ name: “Rahul”, city: “Hyderabad”, mood: “Learning MongoDB ❤️” })
  5. show dbs → see your new database!
  6. db → confirm you’re in class2026Feb14
  7. 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! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *