Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

MongoDB Embedded Documents & Arrays Tutorial : Beginner to Expert


Embedded Documents & Arrays: Nested Magic Boxes in MongoDB

A Russian Doll Adventure - For Beginner to Expert Level


Imagine you have a big magic box (a document). Inside it, you can put smaller boxes (embedded documents) and treasure bags (arrays) that hold many items. No need to open separate boxes in another room.
This is called embedding in MongoDB. Instead of splitting data across many collections (like SQL tables with JOINs), you keep related things together in one document. It is like Russian nesting dolls, everything fits inside perfectly.

This tutorial turns embedding into a fun nesting game, super simple for beginners, but full of pro design patterns for experts.
We shall use our Hero Academy again.

Let’s start nesting!


๐Ÿ“‘ Table of Contents


Part 1: Why Embed? (The Superpower of One-Document Reads)

In SQL → You need multiple tables + JOINs → slow
In MongoDB → Put everything in one document → lightning fast!

Real-Life Examples:

  • A blog post + all its comments
  • A student + all his subjects & marks
  • An order + all items bought

Pros:

  • Atomic updates (everything changes together)
  • Super fast reads (one query gets everything)
  • No JOINs needed

Cons:

  • Document size limit: 16MB
  • Duplication if same data used in many places
  • Harder to query across many parents

Rule of Thumb: Embed when data is always used together and rarely changes independently.


Part 2: Creating Nested Data - Let’s Build Rich Hero Profiles.

use heroAcademy
db.heroes.insertOne({
  name: "Aarav",
  power: "Super Speed",
  level: 85,
  // Embedded Document (smaller box)
  profile: {
    age: 14,
    city: "Mumbai",
    school: "Superhero High"
  },
  // Array (treasure bag)
  skills: ["run", "jump", "quick thinking"],
  // Array of Embedded Documents!
  missions: [
    { name: "Save City", date: ISODate("2025-01-15"), reward: 100 },
    { name: "Stop Train", date: ISODate("2025-03-20"), reward: 150, completed: true }
  ],
  team: {
    name: "Alpha Squad",
    members: ["Priya", "Sanya", "Karan"],
    leader: "Captain Nova"
  }
})

Visual of Nested Document:
Embedded Document Structure
(One document with nested fields and arrays. )

Hero Document
└── {
    name: "Aarav"
    power: "Super Speed"
    level: 85

    profile: {
        age: 14
        city: "Mumbai"
        school: "Superhero High"
    }

    skills: [
        "run",
        "jump",
        "quick thinking"
    ]

    missions: [
        {
            name: "Save City"
            date: 2025-01-15
            reward: 100
        },
        {
            name: "Stop Train"
            date: 2025-03-20
            reward: 150
            completed: true
        }
    ]

    team: {
        name: "Alpha Squad"
        members: ["Priya", "Sanya", "Karan"]
        leader: "Captain Nova"
    }
}

Now the hero’s entire life is in one place!


Part 3: Querying Nested Data - Finding Treasures Inside Boxes

1. Dot Notation – Reach Inside Boxes

// Find heroes from Mumbai
db.heroes.find({ "profile.city": "Mumbai" })
// Find heroes with skill "jump"
db.heroes.find({ skills: "jump" })
// Find heroes who completed a mission
db.heroes.find({ "missions.completed": true })

Beginner Win: Just use dots like opening folders!

2. Exact Array Match

db.heroes.find({ skills: ["run", "jump", "quick thinking"] })

3. $elemMatch - Match Multiple Conditions in Same Array Item

db.heroes.find({
  missions: {
    $elemMatch: { reward: { $gt: 120 }, completed: true }
  }
})

4. $all - Must Have All These Skills

db.heroes.find({ skills: { $all: ["run", "jump"] } })

5. $size - Exact Number of Items

db.heroes.find({ skills: { $size: 3 } })

6. Array Index Position

db.heroes.find({ "skills.0": "run" })  // First skill is "run"

Performance & Indexing Tips for Nested Data

MongoDB automatically creates multikey indexes on arrays, but nested fields often need manual indexing for better performance.

You can speed up nested queries by adding indexes on fields like:

db.heroes.createIndex({ "missions.reward": 1 })
db.heroes.createIndex({ "profile.city": 1 })

Best Practices:

  • Index fields that you frequently query inside embedded documents.
  • Use compound indexes for combined queries (e.g., reward + completion status).
  • Avoid indexing very large arrays, they create heavy multikey indexes.
  • For deep or unpredictable structures, consider referencing instead of embedding.



Part 4: Updating Nested Data - The Magic Paintbrush

1. Update Embedded Field

Example:

db.heroes.updateOne(
  { name: "Aarav" },
  { $set: { "profile.age": 15, "profile.school": "Elite Academy" } }
)

2. Add to Array ($push)

db.heroes.updateOne(
  { name: "Aarav" },
  { $push: { skills: "lightning dash" } }
)

3. Add Multiple ($push + $each)

Example:

db.heroes.updateOne(
  { name: "Aarav" },
  {
    $push: {
      skills: { $each: ["fly", "laser eyes"] }
    }
  }
)

4. Remove from Array ($pull)

Example:

db.heroes.updateOne(
  { name: "Aarav" },
  { $pull: { skills: "jump" } }
)

5. Update Specific Array Element – Positional $ Operator

db.heroes.updateOne(
  { "missions.reward": 100 },
  { $set: { "missions.$.completed": true, "missions.$.reward": 200 } }
)

6. Update All Matching Array Elements ($[])

Example:

db.heroes.updateOne(
  { name: "Aarav" },
  { $inc: { "missions.$[].reward": 50 } }
)

7. Update Specific Element by Condition ($[identifier] + arrayFilters)

Example:

db.heroes.updateOne(
  { name: "Aarav" },
  { $set: { "missions.$[elem].completed": true } },
  { arrayFilters: [ { "elem.reward": { $gte: 150 } } ] }
)

→ Only missions with reward ≥ 150 get completed = true
Expert Power Move!


Part 5: Arrays of Embedded Documents - Real-World Power

Best for:

  • Blog post + comments
  • Order + line items
  • Student + list of subjects with marks

Example:

subjects: [
  { name: "Math", marks: 95, grade: "A+" },
  { name: "Science", marks: 88, grade: "A" }
]

Query:

db.students.find({ "subjects.name": "Math", "subjects.marks": { $gt: 90 } })

Update specific subject:

Example:

db.students.updateOne(
  { name: "Priya" },
  { $set: { "subjects.$.grade": "A++" } },
  { arrayFilters: [ { "subjects.name": "Math" } ] }
)

Part 6: When to Embed vs Reference? (The Golden Rule)

Embed vs Reference (Improved Guide)

Use Embedding When... Use Referencing When...
Data is always read together Child data is queried independently
One-to-few relationship (e.g., comments, profile details) One-to-many with many items (e.g., thousands of orders)
Child changes rarely and depends on parent Child changes frequently on its own
You need atomic updates Document could grow too large
Document stays well under the 16MB limit Data structure is unpredictable or unbounded

Pro Pattern: Hybrid, Embed frequently accessed data, reference rarely changed or huge data.

Example: Embed address in user (changes rarely), reference orders (many, queried separately).


Part 7: Mini Project - Build a Complete Hero Card!

db.heroes.insertOne({
  name: "YouTheReader",
  power: "Learning MongoDB",
  level: 100,
  profile: {
    age: "Ageless",
    location: "Everywhere"
  },
  achievements: [
    "Finished Embedding Tutorial",
    "Understood $elemMatch",
    "Used Positional Operator"
  ],
  superMoves: [
    { name: "Query Storm", power: 999, cooldown: 0 },
    { name: "Index Blitz", power: 1000, cooldown: 5 }
  ]
})

Now try these queries:

db.heroes.find(
  { "superMoves.power": { $gt: 900 } },
  { name: 1, "superMoves.$": 1 }   // Only show matching array elements!
)

Part 8: Tips for All Levels

For Students & Beginners

  • Start with simple nesting: one embedded object + one array
  • Use Compass → you can click into nested fields!
  • Practice with your own “Game Character” document

For Medium Learners

  • Always use $elemMatch when multiple conditions on same array element
  • Use positional $[] for updating all matching array items
  • Remember document 16MB limit!

For Experts

  • Use multikey indexes automatically created on arrays
  • For large arrays > 100 items → consider child collection
  • Use $filter in aggregation to process arrays:
{
  $project: {
    highRewardMissions: {
      $filter: {
        input: "$missions",
        as: "m",
        cond: { $gte: ["$$m.reward", 150] }
      }
    }
  }
}

Schema validation for nested data:

validator: {
  $jsonSchema: {
    properties: {
      profile: { bsonType: "object" },
      skills: { bsonType: "array", items: { bsonType: "string" } }
    }
  }
}



Part 9: Cheat Sheet (Print & Stick!)

TaskCommand Example
Query nested field{ "profile.city": "Mumbai" }
Query array item{ skills: "fly" }
Exact array{ skills: ["a", "b"] }
Multiple array conditions{ array: { $elemMatch: { a: 1, b: 2 } } }
Update nested{ $set: { "profile.age": 16 } }
Add to array{ $push: { skills: "new" } }
Remove from array{ $pull: { skills: "old" } }
Update matched array element"missions.$" with filter
Update all array elements"missions.$[]"

⚡ Quick Summary

  • MongoDB embedding lets you store related data inside a single document (like Russian nesting dolls).
  • Use embedded documents for structured nested data.
  • Use arrays for multiple values or lists of objects.
  • Dot notation ("profile.city": "Mumbai") makes nested queries easy.
  • Array operators such as $elemMatch, $all, $size, $push, $pull, and positional $ give powerful control.
  • Embed when data is small, always read together, and rarely updated independently.
  • Reference when data is large, independently updated, or frequently queried alone.

๐Ÿงช Test Yourself

Try these challenges to test your understanding:

  1. Create a student document containing:
    • an embedded profile object
    • a subjects array (each subject is an embedded document)
    • a hobbies array
  2. Query students who have a subject named "Math" with marks greater than 80.
  3. Update all subject marks by +5 using the $[] operator.
  4. Remove the hobby "gaming" from the hobbies array.
  5. Add two new subjects to the subjects array using $push with $each.

If you can solve these, you're well on your way to mastering MongoDB nesting!


๐Ÿ’ก Common Mistakes

  • Not using $elemMatch when applying multiple conditions to a single array element.
  • Updating arrays without positional operators such as $, $[], or $[identifier].
  • Embedding huge arrays that may grow into hundreds or thousands of items.
  • Duplicating data by embedding objects that should be referenced instead.
  • Ignoring the 16MB document limit, especially when storing logs or long lists.

❗ Things to Avoid When Embedding

  • Embedding large collections such as thousands of comments.
  • Embedding data that changes frequently on its own.
  • Embedding child items you often query independently.
  • Embedding arrays or structures that can grow unpredictably.
  • Embedding complex structures that rely on dynamic keys.

Golden Rule:
Embed when data is small and tightly related.
Reference when data is large, independent, or often queried separately.


Final Words

You’re a Nesting Master.

You just learned:

  • How to build rich, nested documents
  • Query with dot notation, $elemMatch, $all
  • Update with $push, positional operators, arrayFilters
  • When to embed vs reference (the most important design decision!)

Your Nesting Mission:
Create a document about your favorite game character with:

  • Embedded stats object
  • inventory array
  • quests array of objects

You’re now a Certified MongoDB Russian Doll Architect.

Resources:
Embedded vs Reference Docs (official MongoDB guide)
MongoDB Array & Update Operators – Positional Operator $
MongoDB Data Modeling & Embedding Best Practices

Array Operators
Positional Operator
Keep nesting like a pro.

Featured Post

MongoDB Embedded Documents & Arrays Tutorial : Beginner to Expert

Embedded Documents & Arrays: Nested Magic Boxes in MongoDB A Russian Doll Adventure - For Beginner to Expert Level Imagine you hav...

Popular Posts