Skip to content

Latest commit

 

History

History
191 lines (143 loc) · 9.06 KB

File metadata and controls

191 lines (143 loc) · 9.06 KB

MongoDB Tutorial in 1 Hour (2024)

  • Platform: YouTube
  • Channel/Creator: CodeWithHarry
  • Duration: 01:22:57
  • Release Date: May 10, 2023
  • Video Link: Watch on YouTube

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

What is MongoDB?

MongoDB is a document-oriented NoSQL database that stores data in flexible, JSON-like BSON documents. Unlike traditional SQL databases with rigid tables and rows, MongoDB uses databases → collections → documents. This schema-less design means every document in a collection can have completely different fields — perfect when your data structure evolves over time.

Key advantage: incredible scalability and flexibility for modern apps.

Ask AI: MongoDB vs SQL databases

Installing MongoDB Community Server Locally

Download the Community Server from the official MongoDB website → Products → Community Server → choose your OS.

  • Mac: either manual download or brew tap mongodb/brewbrew install mongodb-community
  • Windows: run the .msi installer, select “Install MongoDB as a Service” so it runs in the background.

After installation, run mongod (the daemon) to start the server. By default it looks for data in /data/db (create it if missing) or specify with --dbpath /your/path.

MongoDB Compass (GUI) installs automatically on Windows and can be downloaded separately on Mac.

Ask AI: MongoDB local installation

MongoDB Compass – The GUI Tool

Compass is a free GUI that lets you visually explore, query, and manage your data.

  • Connect to mongodb://localhost:27017
  • Create databases/collections
  • Insert documents manually
  • See instantly how MongoDB is schema-flexible (one document can have fields another doesn’t)

Great for beginners and debugging.

Ask AI: MongoDB Compass tutorial

Core Concepts – Databases, Collections, Documents

SQL MongoDB
Database Database
Table Collection
Row Document (BSON)

Documents look like JSON:

{
  "_id": ObjectId("..."),
  "name": "Harry",
  "role": "Instructor",
  "location": "Delhi"
}

No enforced schema → add or remove fields anytime.

Ask AI: MongoDB core concepts

CRUD Operations in Mongo Shell

Start the shell with mongosh (or mongo in older versions).

show dbs                    // list databases
use employees               // switch/create DB (created on first insert)

db.managers.insertOne({ name: "Rohan", role: "Programmer" })
db.managers.insertMany([ …array of objects… ])

db.managers.find()          // all documents
db.managers.find().pretty() // formatted output
db.managers.find({ role: "Programmer" }) // filter

db.inventory.updateOne(
  { item: "paper" },
  { $set: { "size.uom": "cm", status: "P" } }
)

db.inventory.deleteMany({ status: "D" })

Ask AI: MongoDB CRUD operations

Sorting, Limiting, Skipping & Pagination

db.collection.find().sort({ qty: -1 })     // descending (-1), ascending (1)
db.collection.find().limit(10)            // only 10 docs
db.collection.find().skip(10)             // skip first 10

// Pagination example (page 2, 10 items per page)
db.posts.find()
        .skip((pageNumber - 1) * 10)
        .limit(10)
        .sort({ createdAt: -1 })

Ask AI: MongoDB pagination and sorting

MongoDB Atlas – Cloud Database

Atlas is the official managed cloud service (free tier available forever).

  1. Sign up at mongodb.com/cloud/atlas
  2. Create a shared cluster (M0 free)
  3. Choose provider/region (closest to your users)
  4. Create database user (username + strong password)
  5. Whitelist your IP or allow access from anywhere (0.0.0.0/0 for testing)
  6. Get connection string → replace <password> → connect with Compass or driver

Perfect for production — no server maintenance, auto-scaling, backups.

Ask AI: MongoDB Atlas setup

Query Operators

Powerful operators make complex queries easy:

db.inventory.find({ qty: { $lt: 30 } })           // less than
db.inventory.find({ qty: { $gt: 50 } })           // greater than
db.inventory.find({ qty: { $lte: 30 } })          // less than or equal
db.inventory.find({ status: { $in: ["A", "P"] } }) // in array
db.inventory.find({ status: { $nin: ["D"] } })    // not in

Ask AI: MongoDB query operators

Aggregation Pipeline

Process data in stages — extremely powerful for analytics.

Example: total quantity per pizza size “Medium”:

db.orders.aggregate([
  { $match: { size: "medium" } },
  {
    $group: {
      _id: "$name",                     // group by pizza name
      totalQuantity: { $sum: "$quantity" }
    }
  },
  { $sort: { totalQuantity: -1 } }
])

Stages run sequentially, each stage receives the output of the previous one.

Ask AI: MongoDB aggregation pipeline


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: