Challenge - 5 Problems
MongoDB One-to-Many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find all orders for a specific customer
Given two collections, customers and orders, where
orders references customers by customer_id, which query returns all orders for the customer with _id equal to 123?Attempts:
2 left
💡 Hint
Think about which collection stores the orders and how they link to customers.
✗ Incorrect
Orders reference customers by customer_id, so to find orders for customer 123, filter orders where customer_id is 123.
📝 Syntax
intermediate2:00remaining
Correct syntax for referencing in MongoDB
Which of the following is the correct way to store a one-to-many reference from a
customer document to multiple order documents using ObjectId references?Attempts:
2 left
💡 Hint
Think about how to store multiple references in an array.
✗ Incorrect
An array of ObjectIds correctly represents multiple references; options A and D use wrong types, C only stores one reference.
❓ optimization
advanced2:00remaining
Efficiently retrieving customer with their orders
You want to retrieve a customer document along with all their orders in one query. Which MongoDB aggregation pipeline stage should you use to join
customers and orders collections on _id and customer_id?Attempts:
2 left
💡 Hint
Look for the stage that performs a left outer join.
✗ Incorrect
$lookup performs a join between collections; other options do not join or filter correctly.
🔧 Debug
advanced2:00remaining
Why does this query return no orders?
A developer runs this query to find orders for customer with _id 'abc123':
db.orders.find({ customer_id: 'abc123' }). But it returns no results, even though orders exist. What is the likely cause?Attempts:
2 left
💡 Hint
Check the data types of the fields used in the query.
✗ Incorrect
MongoDB matches types strictly; querying with a string won't match ObjectId fields.
🧠 Conceptual
expert3:00remaining
Best practice for one-to-many referencing in MongoDB
In a one-to-many relationship where a customer can have thousands of orders, what is the best practice for storing references in MongoDB?
Attempts:
2 left
💡 Hint
Consider document size limits and query efficiency.
✗ Incorrect
Storing customer_id in orders avoids large arrays in customer documents and supports efficient queries.