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.
db.users.find({ "orders.status": "shipped" })Think about how MongoDB matches documents with embedded arrays.
The query matches user documents where the embedded orders array contains at least one element with status equal to 'shipped'. It returns the whole user document, not just the matching orders.
In MongoDB, when would referencing documents be a better modeling choice than embedding?
Think about data size and update frequency.
Referencing is better when related data is large or changes often, so you avoid duplicating data and keep documents smaller. Embedding is better for small, stable related data.
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 }, ??? )Remember how to use the positional operator $ in updates.
The positional operator $ identifies the matched array element in the query filter. Option A correctly uses it to update the status field of the matching order.
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?
Think about document size limits and query performance.
Embedding large arrays can slow queries and hit document size limits. Moving reviews to a separate collection and referencing them improves query speed and scalability.
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?
Consider how embedding duplicates data and update propagation.
Embedding duplicates customer data in each order. If you update the customer address in one order, other orders still have old data, causing inconsistency.