0
0
MongoDBquery~3 mins

Normalization vs denormalization default in MongoDB - When to Use Which

Choose your learning style9 modes available
The Big Idea

Discover how organizing your data smartly can save you hours of frustration and errors!

The Scenario

Imagine you have a big notebook where you write down all your friends' details and their favorite movies. Every time you add a new movie for a friend, you copy the friend's name and details again and again. It becomes hard to find and update information quickly.

The Problem

Writing everything repeatedly wastes space and causes mistakes. If a friend's phone number changes, you must find and update it in many places. This takes a lot of time and can lead to errors, making your notebook messy and confusing.

The Solution

Normalization organizes data by separating it into smaller, linked parts to avoid repetition. Denormalization combines related data to make reading faster. Both methods help keep data clean and easy to manage, depending on what you need most: quick updates or quick reads.

Before vs After
Before
friends = [{name: 'Alice', phone: '123', movies: ['Movie1', 'Movie2']}, {name: 'Alice', phone: '123', movies: ['Movie3']}]
// repeated friend info
After
friends = [{_id: 1, name: 'Alice', phone: '123'}]
movies = [{title: 'Movie1', friend_id: 1}, {title: 'Movie2', friend_id: 1}, {title: 'Movie3', friend_id: 1}]
// linked data without repetition
What It Enables

It allows you to choose the best way to store data for your app's speed and accuracy needs.

Real Life Example

Online stores use normalization to keep customer info separate from orders, so updating an address is easy. But they use denormalization to quickly show order details with customer names on the screen.

Key Takeaways

Normalization reduces repeated data for easy updates.

Denormalization stores combined data for faster reading.

Choosing the right method improves app performance and accuracy.