0
0
MongoDBquery~3 mins

Rows vs documents thinking in MongoDB - When to Use Which

Choose your learning style9 modes available
The Big Idea

What if you could find all your data about a person in one quick step instead of hunting through many places?

The Scenario

Imagine you have a big spreadsheet where each row holds a tiny piece of information about a person, like their name, address, and phone number, but all spread out in different rows. You want to find all details about one person quickly.

The Problem

Looking through many rows to gather all details about one person is slow and confusing. You might miss some rows or mix up data because everything is separated. It's like searching for puzzle pieces scattered all over the table.

The Solution

With document thinking, all information about one person is stored together in one place, like a complete folder. This makes it easy and fast to find everything about that person without searching through many rows.

Before vs After
Before
SELECT name FROM people WHERE id=1;
SELECT address FROM addresses WHERE person_id=1;
SELECT phone FROM phones WHERE person_id=1;
After
db.people.findOne({ _id: 1 })
What It Enables

This way of thinking lets you work with whole sets of related information at once, making your data faster to access and easier to understand.

Real Life Example

Think about an online store: instead of storing customer info, orders, and reviews in separate tables, a document stores all these details together, so the store can quickly show everything about a customer in one go.

Key Takeaways

Rows spread data across many places, making gathering related info slow.

Documents keep related data together, speeding up access and simplifying use.

Choosing the right way to organize data helps your apps run smoother and your work easier.