Lesson 18 of 49 intermediate

MongoDB — The Flexible Database

Store Data Like JSON Files

Open interactive version (quiz + challenge)

Real-world analogy

MySQL is like a strict printed form — every person fills in EXACTLY the same boxes (columns). MongoDB is like a folder of sticky notes — each note can have different info! One has a phone number, another has an address. Both are fine!

What is it?

MongoDB is a NoSQL database that stores data as flexible JSON-like documents (BSON). Unlike SQL databases with rigid tables, MongoDB lets each document have its own structure. It's the 'M' in the MEAN/MERN stack.

Real-world relevance

MongoDB is used by Adobe, eBay, Forbes, Google, and Toyota. It's especially popular for apps with varied data shapes — like property listings, user profiles, or content management systems.

Key points

Code example

// SQL (MySQL) — rigid table structure 📊
// Every row MUST have the same columns:
// | id | name  | email           | age |
// | 1  | Alex  | alex@mail.com   | 25  |
// | 2  | Sam   | sam@mail.com    | 30  |

// MongoDB — flexible documents 📄
// Document 1
{
  "_id": ObjectId("abc123"),
  "name": "Alex",
  "email": "alex@mail.com",
  "age": 25
}
// Document 2 — has EXTRA fields, and that's OK!
{
  "_id": ObjectId("def456"),
  "name": "Sam",
  "email": "sam@mail.com",
  "age": 30,
  "phone": "+1234567",
  "address": {
    "city": "New York",
    "country": "USA"
  },
  "hobbies": ["coding", "gaming", "cooking"]
}

// Nested data — no JOINs needed! 🎯
{
  "_id": ObjectId("prop123"),
  "name": "Sunset Apartment",
  "address": {
    "street": "123 Main St",
    "city": "San Francisco",
    "coordinates": { "lat": 37.77, "lng": -122.41 }
  },
  "images": [
    { "url": "photo1.jpg", "caption": "Living room" },
    { "url": "photo2.jpg", "caption": "Kitchen" }
  ],
  "amenities": ["wifi", "parking", "gym"],
  "price": 2500,
  "available": true
}

// MongoDB queries with Prisma
const user = await prisma.user.create({
  data: {
    name: "John",
    email: "john@mail.com",
    age: 25
  }
});

// Find with flexible filtering
const adults = await prisma.user.findMany({
  where: { age: { gte: 18 } }
});

// Update nested data
await prisma.property.update({
  where: { id: "prop123" },
  data: {
    address: {
      city: "Los Angeles"
    }
  }
});

Line-by-line walkthrough

  1. 1. SQL (MySQL) — rigid table structure 📊
  2. 2. Every row MUST have the same columns:
  3. 3. | id | name | email | age |
  4. 4. | 1 | Alex | alex@mail.com | 25 |
  5. 5. | 2 | Sam | sam@mail.com | 30 |
  6. 6.
  7. 7. MongoDB — flexible documents 📄
  8. 8. Document 1
  9. 9. Opening block
  10. 10.
  11. 11.
  12. 12.
  13. 13.
  14. 14. Closing block
  15. 15. Document 2 — has EXTRA fields, and that's OK!
  16. 16. Opening block
  17. 17.
  18. 18.
  19. 19.
  20. 20.
  21. 21.
  22. 22.
  23. 23.
  24. 24.
  25. 25. Closing block
  26. 26.
  27. 27. Closing block
  28. 28.
  29. 29. Nested data — no JOINs needed! 🎯
  30. 30. Opening block
  31. 31.
  32. 32.
  33. 33.
  34. 34.
  35. 35.
  36. 36.
  37. 37. Closing block
  38. 38.
  39. 39.
  40. 40.
  41. 41.
  42. 42.
  43. 43.
  44. 44.
  45. 45. Closing block
  46. 46.
  47. 47. MongoDB queries with Prisma
  48. 48. Declaring a variable
  49. 49.
  50. 50.
  51. 51.
  52. 52.
  53. 53. Closing block
  54. 54.
  55. 55.
  56. 56. Find with flexible filtering
  57. 57. Declaring a variable
  58. 58.
  59. 59.
  60. 60.
  61. 61. Update nested data
  62. 62. Waiting for an async operation to complete
  63. 63.
  64. 64.
  65. 65.
  66. 66.
  67. 67. Closing block
  68. 68. Closing block
  69. 69.

Spot the bug

const user = await prisma.user.findUnique({
  where: { name: "John" }
});
Need a hint?
What kind of field does findUnique require?
Show answer
findUnique requires a unique field (like id or @unique field). 'name' is usually not unique. Fix: use findFirst for non-unique fields, or use id/email instead.

Explain like I'm 5

Imagine a folder full of sticky notes. Each note can have different things - one has a phone number, another has a drawing. That's MongoDB! Unlike a strict notebook with lines and columns (SQL), your sticky notes can have whatever you want.

Fun fact

MongoDB's name comes from 'humongous' — because it was designed to handle HUMONGOUS amounts of data. The founders literally just shortened the word! 🦕

Hands-on challenge

Design a schema for a movie database: each movie has a title, year, genres (array), and a nested 'ratings' object with { imdb: number, rottenTomatoes: number }. Insert 5 movies, then write a query to find all movies after 2020 with an IMDB rating above 7. Can you sort them by year descending?

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook