0
0
MongoDBquery~20 mins

Schema design for read-heavy workloads in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Read-Heavy Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why embed documents in MongoDB for read-heavy workloads?

In MongoDB, when designing a schema for a read-heavy application, why is embedding related data inside a single document often preferred over referencing separate documents?

AEmbedding reduces the number of queries needed to fetch related data, improving read performance.
BEmbedding increases the size of each document, which slows down reads significantly.
CEmbedding forces MongoDB to create multiple indexes, which hurts read speed.
DEmbedding requires more disk space, so it is avoided in read-heavy workloads.
Attempts:
2 left
💡 Hint

Think about how many database calls are needed to get all related data.

query_result
intermediate
2:00remaining
Result of a MongoDB query with embedded documents

Given a collection users where each user document embeds an array of orders, what will be the output of this query?

db.users.find({"orders.status": "shipped"}, {name: 1, orders: 1})

Assuming the collection has:

{ "name": "Alice", "orders": [{"id": 1, "status": "shipped"}, {"id": 2, "status": "pending"}] }
{ "name": "Bob", "orders": [{"id": 3, "status": "pending"}] }
A[{ "name": "Alice", "orders": [{"id": 1, "status": "shipped"}] }]
B[]
C[{ "name": "Bob", "orders": [{"id": 3, "status": "pending"}] }]
D[{ "name": "Alice", "orders": [{"id": 1, "status": "shipped"}, {"id": 2, "status": "pending"}] }]
Attempts:
2 left
💡 Hint

Remember that MongoDB returns the whole document if any embedded document matches the query.

📝 Syntax
advanced
2:00remaining
Correct schema design for fast reads with large arrays

Which MongoDB schema design is best for a read-heavy workload where each user has thousands of comments, and you want to quickly fetch the latest 10 comments?

AEmbed all comments inside the user document in a single array.
BStore comments in a separate collection with a user_id field and index on user_id and timestamp.
CEmbed comments in multiple arrays inside the user document, splitting by year.
DStore comments in a separate collection without any indexes.
Attempts:
2 left
💡 Hint

Think about document size limits and efficient querying.

optimization
advanced
2:00remaining
Optimizing read performance with indexes in MongoDB

You have a collection products with fields category, price, and rating. Your app frequently queries products by category and sorts by price ascending. Which index will optimize these queries best?

ACreate a compound index on { price: 1, category: 1 }
BCreate a single index on price only
CCreate a compound index on { category: 1, price: 1 }
DCreate a single index on category only
Attempts:
2 left
💡 Hint

Consider the order of fields in compound indexes for filtering and sorting.

🔧 Debug
expert
2:00remaining
Why does this MongoDB query run slowly on a read-heavy workload?

Consider this query on a large orders collection:

db.orders.find({ status: "completed" }).sort({ orderDate: -1 }).limit(5)

The query is slow despite indexes on status and orderDate separately. What is the main reason?

AMongoDB cannot use two separate indexes efficiently for filtering and sorting; a compound index is needed.
BThe limit(5) clause causes MongoDB to scan the entire collection.
CIndexes on individual fields always slow down queries with sort.
DThe query syntax is invalid and causes a full collection scan.
Attempts:
2 left
💡 Hint

Think about how MongoDB uses indexes for filtering and sorting together.