0
0
MongoDBquery~20 mins

Why modeling decisions matter in MongoDB - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Modeling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
How does embedding affect query results?

Consider a MongoDB collection users where each user document embeds an array of orders. What will be the output of the following query?

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

Choose the correct description of the result.

MongoDB
db.users.find({ "orders.status": "shipped" })
AReturns all users regardless of order status.
BReturns only the orders with status 'shipped' from all users.
CReturns all users who have at least one order with status 'shipped'.
DReturns an error because you cannot query embedded arrays.
Attempts:
2 left
💡 Hint

Think about how MongoDB matches documents with embedded arrays.

🧠 Conceptual
intermediate
2:00remaining
Why choose referencing over embedding?

In MongoDB, when would referencing documents be a better modeling choice than embedding?

AWhen you want to avoid using indexes on related fields.
BWhen related data is large and changes frequently, and you want to avoid duplicating data.
CWhen you want to store all related data in one document for faster reads.
DWhen you want to prevent any relationships between collections.
Attempts:
2 left
💡 Hint

Think about data size and update frequency.

📝 Syntax
advanced
2:00remaining
Identify the correct update syntax for embedded documents

Which of the following MongoDB update commands correctly sets the status field to 'delivered' for the order with orderId 123 inside the embedded orders array?

db.users.updateOne({ _id: 1, "orders.orderId": 123 }, ??? )
A{ $set: { "orders.$.status": "delivered" } }
B{ $set: { "orders.status": "delivered" } }
C{ $update: { "orders.$.status": "delivered" } }
D{ $set: { "orders[orderId=123].status": "delivered" } }
Attempts:
2 left
💡 Hint

Remember how to use the positional operator $ in updates.

optimization
advanced
2:00remaining
Optimizing queries with large embedded arrays

You have a products collection where each product document embeds a large array of reviews. Queries filtering by review rating are slow. What is the best way to optimize these queries?

AMove <code>reviews</code> to a separate collection and reference them from <code>products</code>.
BIncrease the size limit of documents to hold more reviews.
CEmbed more fields inside each review to avoid joins.
DRemove the <code>reviews</code> array and store reviews in a text field.
Attempts:
2 left
💡 Hint

Think about document size limits and query performance.

🔧 Debug
expert
3:00remaining
Diagnose the cause of inconsistent data in embedded documents

A MongoDB collection orders embeds customer details inside each order document. After some updates, customer addresses are inconsistent across orders for the same customer. What is the main reason for this inconsistency?

AMongoDB does not support embedded documents for customer data.
BThe database automatically synchronizes embedded data, so inconsistency is impossible.
CReferencing customer data inside orders causes inconsistent addresses.
DEmbedding customer data causes duplication, so updates to one order do not affect others.
Attempts:
2 left
💡 Hint

Consider how embedding duplicates data and update propagation.