0
0
MongoDBquery~3 mins

Why Embedded documents (nested objects) in MongoDB? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how keeping related data together can save you hours of searching and confusion!

The Scenario

Imagine you have a big filing cabinet with many folders, and each folder has papers about a person and their family members. To find details about a family member, you have to open many folders and shuffle through papers manually.

The Problem

This manual way is slow and confusing. You might lose papers or mix up information. It's hard to keep track of who belongs to which family, and updating details means searching through many folders again and again.

The Solution

Embedded documents let you keep related information together in one place, like putting all family members' details inside one folder. This way, you can find and update everything quickly without searching through many places.

Before vs After
Before
{ name: 'John', family_member1: 'Anna', family_member2: 'Mike' }
After
{ name: 'John', family: [{ name: 'Anna' }, { name: 'Mike' }] }
What It Enables

It enables storing and accessing related data as a single unit, making your database faster and easier to manage.

Real Life Example

Think of a social media app where each user has posts and comments. Embedded documents let you store all posts and comments inside the user's profile, so you can load everything about a user quickly.

Key Takeaways

Embedded documents group related data together.

This reduces searching and speeds up data access.

It simplifies updating and organizing complex information.