Chapter 5: MongoDB mongosh Create Collection

What is MongoDB mongosh Create Collection?

Just like databases, MongoDB is very relaxed about collections too (collections = tables in SQL world).

Big truth first (write in big letters!):

MongoDB does NOT require you to create a collection before using it. Most of the time, you never explicitly create a collection — it magically appears the first time you insert a document into it.

But sometimes you want to create it explicitly — especially when you need special settings like:

  • Capped collections (fixed size, auto-deletes old data like a circular log)
  • Time-series collections (for IoT/sensor data, optimized storage)
  • Schema validation (force certain fields/types — like “light schema”)
  • TTL indexes (auto-expire documents after time)
  • Specific storage options, collation, etc.

The command for explicit creation in mongosh is: db.createCollection(“collectionName”, { options })

Two Ways to “Create” a Collection in mongosh

Way How it happens When to use Command Example
Implicit (Automatic – 95% of cases) Happens when you first insertOne(), insertMany(), etc. Everyday apps, quick prototyping db.students.insertOne({ name: “Rahul” }) → creates students if missing
Explicit (Using createCollection) You run db.createCollection() first Need special options (capped, time-series, validation, etc.) db.createCollection(“logs”, { capped: true, size: 1000000 })

Hands-on – Let’s Do Both Ways Right Now

Assume you’re already connected in mongosh and switched to a database:

JavaScript

Way 1: Implicit Creation (Super Simple – Do This Most of the Time)

JavaScript

Now check:

JavaScript

You should see students listed!

→ No createCollection needed. MongoDB handles it for you.

Way 2: Explicit Creation with db.createCollection() (When You Need Control)

Basic empty collection (rarely needed, but good to know):

JavaScript

Now show collections → you’ll see teachers even though it’s empty.

But the real power is in options:

Example 1: Capped Collection (Fixed size, oldest data auto-deletes – great for logs)

JavaScript

After creation:

JavaScript

Insert some logs:

JavaScript

When it fills up → oldest documents disappear automatically. Like a rolling window.

Example 2: Time-Series Collection (Very popular in 2026 for metrics, IoT, stock prices)

JavaScript

Insert example:

JavaScript

→ Optimized storage, faster range queries on time.

Example 3: With Schema Validation (Enforce some rules – like “light contract”)

JavaScript

Now if you try to insert invalid data:

JavaScript

→ Great for preventing bad data in production.

Quick Summary Table

Feature You Want Use Implicit? Use db.createCollection? Example Command Snippet
Normal flexible collection Yes No (optional) db.items.insertOne({…})
Capped / fixed-size log No Yes { capped: true, size: 1e6 }
Time-series (sensor/metric data) No Yes { timeseries: { timeField: “ts” } }
Enforce schema rules No Yes { validator: { $jsonSchema: … } }
TTL auto-expire documents No (index) Sometimes Create TTL index after
Just want empty collection first No Yes db.createCollection(“temp”)

How to Check / List Collections

JavaScript

Mini Exercise – Try Now!

  1. use myValentineDb
  2. Create a capped loveNotes collection:
JavaScript
  1. Insert few notes:
JavaScript
  1. show collections → see it!
  2. Insert 100+ small docs → watch oldest disappear when size limit hits.

Understood everything? This is one of those “MongoDB is flexible but gives power when needed” moments.

Next class options:

  • How to drop a collection safely?
  • Rename collection?
  • Create indexes for fast queries?
  • Schema validation deep dive?
  • Or start a small project like “Valentine Wishlist App” with collections?

Tell me what you want next — class is open! 🚀😄

Any doubt in today’s topic? Ask freely!

You may also like...

Leave a Reply

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