Hint: Denormalization embeds related data inside one document [OK]
Common Mistakes:
Choosing separate collections as denormalized
Ignoring embedded arrays as denormalization
Confusing null fields with embedded data
3. Given these two collections: 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?
medium
A. It requires multiple queries or a join-like operation to get all orders for a user.
B. It duplicates order data inside each user document.
C. It stores all orders inside the user document causing large documents.
D. It prevents updating user names easily.
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 A
Quick Check:
Normalized read = multiple queries [OK]
Hint: Normalized data needs multiple queries to combine related info [OK]
Common Mistakes:
Thinking normalized data duplicates info
Assuming all data is embedded in one document
Believing updates are harder in normalized data
4. You have a denormalized MongoDB document: { _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?
medium
A. Query performance slows down because of references.
B. Indexes on orders array are lost.
C. The database schema becomes normalized automatically.
D. Data inconsistency due to duplicated order info in multiple documents.
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 D
Quick Check:
Denormalization risk = data inconsistency [OK]
Hint: Denormalization can cause inconsistent duplicated data if not updated everywhere [OK]
Common Mistakes:
Thinking denormalization slows queries
Believing schema changes automatically
Confusing index loss with denormalization
5. You want to design a MongoDB schema for a blog with users and posts. 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.
hard
A. Separate collections for users and posts for easy updates.
B. Store posts only with duplicated user info for simpler queries.
C. Embed posts inside user documents for fast reads since posts rarely change.
D. Duplicate posts in both collections to optimize writes.
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 C
Quick Check:
Denormalization + rare updates = embed for fast reads [OK]
Hint: Embed rarely changing related data for faster reads [OK]