MongoDB Tutorial

1. What exactly is MongoDB? (The Big Picture First)

Imagine you are running an online book store (like Amazon but small scale).

In traditional SQL databases (MySQL, PostgreSQL):

  • You need to decide every column before storing data → title, author, price, pages, isbn, publish_year, etc.
  • Every book must have all these fields (or NULL).
  • If tomorrow you want to add “audio_length_minutes” for audiobooks → you have to ALTER TABLE (painful for large tables).
  • You store data in very strict rows & columns (like Excel sheet with fixed structure).

MongoDB says: “Relax yaar!”

  • It is a NoSQL document database.
  • Data is stored as flexible JSON-like documents.
  • No fixed schema → different documents in same collection can have different fields.
  • Perfect for modern apps: mobile apps, e-commerce, IoT, content management, real-time analytics, etc.

Official definition (2026 style):

MongoDB is a source-available, cross-platform, document-oriented NoSQL database that uses JSON-like BSON documents with optional schemas.

2. Key Concepts – Analogy Time

Think of a school:

SQL (MySQL) MongoDB School Analogy
Database Database One entire school
Table Collection One class (e.g., 10th A)
Row / Record Document One student file
Column / Field Field One detail (name, marks, hobby, blood group)
Schema Schema-less / Flexible Some students have medical notes, some don’t

Example student document in MongoDB:

JSON

Next student in same collection:

JSON

See? No stress about missing fields or adding new ones.

3. Let’s Start Practically – Installation Options (2026)

Three popular ways beginners use today:

Option A – Easiest (Recommended for learning) Use MongoDB Atlas (cloud – free forever for small projects)

  1. Go to → https://www.mongodb.com/cloud/atlas
  2. Sign up (Google/GitHub/email)
  3. Create free cluster (M0 free tier – enough for learning)
  4. Create database user
  5. Get connection string (looks like: mongodb+srv://username:password@cluster0.abcde.mongodb.net/myFirstDatabase?retryWrites=true&w=majority)

Option B – Local (good for offline practice)

  • Download MongoDB Community Server: https://www.mongodb.com/try/download/community
  • Or use Docker (very popular in 2026):
Bash

Option C – MongoDB Shell / mongosh (command line tool)

Comes with installation or install separately.

4. CRUD Operations – The Heart of MongoDB (Hands-on)

Open mongosh (new name of mongo shell) or use MongoDB Compass (GUI – very beginner friendly).

Let’s create our school database.

JavaScript

See output → it gives you insertedId or insertedIds.

JavaScript
JavaScript
JavaScript

5. Most Important Query Operators (You will use these daily)

Operator Meaning Example
$eq equal { age: { $eq: 16 } }
$gt / $gte greater / greater or equal { “marks.math”: { $gt: 85 } }
$in value in array { city: { $in: [“Hyderabad”, “Bangalore”] } }
$and / $or logical { $and: [ {age: {$gt:15}}, {city:”Hyderabad”} ] }
$exists field exists { hobby: { $exists: true } }

6. Quick Mini Project – Let’s Build Something Useful

Imagine we’re making a movie review app.

JavaScript

Quick Summary Table – What You Learned Today

Task Command Example
Use/Create DB use mydb
Insert one db.collection.insertOne({…})
Insert many db.collection.insertMany([{}, {}])
Find all db.collection.find()
Find with condition db.collection.find({ age: { $gt: 18 } })
Update db.collection.updateOne(filter, { $set: {..} })
Delete db.collection.deleteMany({ city: “Delhi” })

Want to go deeper next? Tell me:

  • Want to learn Aggregation Pipeline (very powerful – like GROUP BY + more)?
  • Indexing for fast search?
  • How to connect MongoDB with Node.js / Python / Java?
  • Atlas search / Vector search (very hot in 2026 for AI apps)?

Just say the word — we’ll continue the class! 🚀

Any doubts so far? Ask anything — no question is silly. 😄