Discover how organizing your data smartly can save you hours of frustration and errors!
Normalization vs denormalization default in MongoDB - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.
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.
friends = [{name: 'Alice', phone: '123', movies: ['Movie1', 'Movie2']}, {name: 'Alice', phone: '123', movies: ['Movie3']}]
// repeated friend infofriends = [{_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 repetitionIt allows you to choose the best way to store data for your app's speed and accuracy needs.
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.
Normalization reduces repeated data for easy updates.
Denormalization stores combined data for faster reading.
Choosing the right method improves app performance and accuracy.
Practice
Solution
Step 1: Understand normalization concept
Normalization means splitting data into separate collections and linking them by references.Step 2: Identify the main benefit
This separation makes updating data easier because changes happen in one place without duplication.Final Answer:
It separates data into collections linked by references for easy updates. -> Option AQuick Check:
Normalization = separate collections + easy updates [OK]
- Confusing normalization with denormalization
- Thinking normalization duplicates data
- Assuming normalization speeds up reads
Solution
Step 1: Identify denormalized structure
Denormalization stores related data together inside one document, like embedding orders inside user.Step 2: Check options for embedded data
{ _id: 1, name: 'Alice', orders: [ { orderId: 101, item: 'Book' } ] } embeds orders array inside the user document, showing denormalization.Final Answer:
{ _id: 1, name: 'Alice', orders: [ { orderId: 101, item: 'Book' } ] } -> Option BQuick Check:
Denormalization = embedded related data [OK]
- Choosing separate collections as denormalized
- Ignoring embedded arrays as denormalization
- Confusing null fields with embedded data
users: { _id: 1, name: 'Bob' }orders: { _id: 101, userId: 1, item: 'Pen' }What is the main drawback of this normalized design when reading user orders?
Solution
Step 1: Understand normalized design
Users and orders are in separate collections linked by userId reference.Step 2: Identify drawback when reading
To get all orders for a user, you must query orders collection filtering by userId, requiring multiple queries or aggregation.Final Answer:
It requires multiple queries or a join-like operation to get all orders for a user. -> Option AQuick Check:
Normalized read = multiple queries [OK]
- Thinking normalized data duplicates info
- Assuming all data is embedded in one document
- Believing updates are harder in normalized data
{ _id: 1, name: 'Carol', orders: [ { orderId: 201, item: 'Notebook' } ] }Which problem can occur if you update the item name in one order but forget to update it elsewhere?
Solution
Step 1: Recognize denormalization risk
Denormalization duplicates related data inside documents, so the same order info may appear in many places.Step 2: Understand update problem
If you update one copy but not others, data becomes inconsistent and unreliable.Final Answer:
Data inconsistency due to duplicated order info in multiple documents. -> Option DQuick Check:
Denormalization risk = data inconsistency [OK]
- Thinking denormalization slows queries
- Believing schema changes automatically
- Confusing index loss with denormalization
Users have many posts, and posts rarely change after creation.
Which design is best for fast reading and why?
Options:
A: Store users and posts in separate collections (normalized).B: Embed all posts inside each user document (denormalized).C: Duplicate posts in both users and posts collections.D: Store posts only, with user info duplicated in each post.Solution
Step 1: Analyze data change frequency
Posts rarely change, so embedding them inside users won't cause frequent update problems.Step 2: Choose design for fast reads
Embedding posts inside user documents allows fetching user and posts in one read, improving read speed.Step 3: Compare options
Embedding posts inside user documents for fast reads since posts rarely change fits best for fast reads with rare updates; separate collections require joins; duplicating posts in both risks inconsistency; storing posts only duplicates user info unnecessarily.Final Answer:
Embed posts inside user documents for fast reads since posts rarely change. -> Option CQuick Check:
Denormalization + rare updates = embed for fast reads [OK]
- Choosing normalization for fast reads
- Duplicating data causing inconsistency
- Ignoring update frequency in design
