We design database schemas to make reading data fast and easy. This helps when many people want to get data quickly.
Schema design for read-heavy workloads in MongoDB
No fixed code syntax because schema design is about how you organize data in collections and documents.MongoDB stores data in collections with flexible documents (like JSON).
Design focuses on embedding related data or referencing it to optimize reads.
{
_id: ObjectId("..."),
name: "Alice",
orders: [
{ order_id: 1, product: "Book", quantity: 2 },
{ order_id: 2, product: "Pen", quantity: 5 }
]
}{
_id: ObjectId("..."),
name: "Bob",
order_ids: [ObjectId("..."), ObjectId("...")]
}
// Orders stored in separate collectionThis example shows embedding orders inside the user document to get all data with one query, which is fast for reading.
db.users.insertOne({
_id: ObjectId("64a1f0c2f1a2b3c4d5e6f7a8"),
name: "Alice",
orders: [
{ order_id: 1, product: "Book", quantity: 2 },
{ order_id: 2, product: "Pen", quantity: 5 }
]
})
// Query to get user and all orders in one go
const user = db.users.findOne({ name: "Alice" })
printjson(user);Embedding data reduces the number of queries needed to read related information.
Too much embedding can make documents large and slow to write.
Referencing is better if related data changes often or is very large.
Design schema to make reading data fast and simple.
Use embedding to keep related data together for quick reads.
Use referencing when data is large or changes often to keep documents small.
Practice
read-heavy workloads in MongoDB?Solution
Step 1: Understand read-heavy workload needs
Read-heavy workloads benefit from fewer queries and faster data retrieval.Step 2: Choose schema design for fast reads
Embedding related data in one document reduces the need for multiple queries, speeding up reads.Final Answer:
Embed related data in the same document to reduce read operations -> Option AQuick Check:
Embedding = faster reads [OK]
- Choosing referencing for all data slows reads
- Normalizing like SQL increases read complexity
- Separating all data causes many queries
Solution
Step 1: Identify correct array embedding syntax
Embedding an array of subdocuments requires an array of objects inside the field.Step 2: Check each option's structure
{ title: 'Post', comments: [{ user: 'Alice', text: 'Nice!' }, { user: 'Bob', text: 'Great!' }] } correctly uses an array of objects for comments; others use incorrect types or formats.Final Answer:
{ title: 'Post', comments: [{ user: 'Alice', text: 'Nice!' }, { user: 'Bob', text: 'Great!' }] } -> Option AQuick Check:
Array of objects = correct embedding [OK]
- Using string instead of array for multiple comments
- Embedding a single object instead of an array
- Using array of strings instead of objects
{ _id: 1, name: 'Gadget', reviews: [{ user: 'Ann', rating: 5 }, { user: 'Ben', rating: 4 }] }What will be the result of this query?
db.products.findOne({ _id: 1 }, { name: 1, 'reviews.rating': 1 })Solution
Step 1: Understand projection with dot notation
Using 'reviews.rating': 1 includes only the rating field inside each review subdocument.Step 2: Analyze returned fields
The query returns _id, name, and reviews with only rating fields (user excluded).Final Answer:
{ _id: 1, name: 'Gadget', reviews: [{ rating: 5 }, { rating: 4 }] } -> Option BQuick Check:
Projection with dot notation = filtered subfields [OK]
- Expecting full subdocuments when projecting subfields
- Forgetting _id is included by default
- Assuming projection excludes _id without explicit exclusion
Solution
Step 1: Identify embedding drawbacks
Embedding large or frequently changing data can cause large documents and slow updates or queries.Step 2: Choose referencing for large or mutable data
Referencing addresses separately keeps user documents small and improves query speed on addresses.Final Answer:
Addresses should be referenced separately if they are large or updated often -> Option DQuick Check:
Large mutable data = use referencing [OK]
- Assuming embedding always improves speed
- Thinking MongoDB disallows embedding arrays
- Believing separate databases are needed for performance
Solution
Step 1: Analyze data change frequency
Posts change often; embedding them inside profiles would cause large documents and frequent updates.Step 2: Choose referencing for frequently changing large data
Storing posts separately and referencing them keeps profiles small and reads fast.Step 3: Avoid data duplication to prevent sync issues
Duplicating posts in profiles causes update complexity and inconsistency risks.Final Answer:
Store user profiles and posts in separate collections, referencing posts from profiles -> Option CQuick Check:
Frequent changes = referencing large data [OK]
- Embedding frequently changing posts inside profiles
- Duplicating data causing sync problems
- Embedding profiles inside posts which is illogical
Ready to prove it?
Pass the test and this pattern moves unknown
