0
0
MongoDBquery~20 mins

One-to-many referencing pattern in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB One-to-Many Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
Adb.orders.find({ _id: 123 })
Bdb.customers.find({ _id: 123 }).orders
Cdb.orders.find({ customer_id: 123 })
Ddb.orders.aggregate([{ $match: { _id: 123 } }])
Attempts:
2 left
💡 Hint
Think about which collection stores the orders and how they link to customers.
📝 Syntax
intermediate
2: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?
A{ _id: ObjectId('abc'), orders: { o1: ObjectId('o1'), o2: ObjectId('o2') } }
B{ _id: ObjectId('abc'), orders: 'o1,o2' }
C{ _id: ObjectId('abc'), orders: ObjectId('o1') }
D{ _id: ObjectId('abc'), orders: [ObjectId('o1'), ObjectId('o2')] }
Attempts:
2 left
💡 Hint
Think about how to store multiple references in an array.
optimization
advanced
2: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?
A{ $group: { _id: '$customer_id', orders: { $push: '$$ROOT' } } }
B{ $lookup: { from: 'orders', localField: '_id', foreignField: 'customer_id', as: 'orders' } }
C{ $match: { customer_id: '_id' } }
D{ $project: { orders: 1 } }
Attempts:
2 left
💡 Hint
Look for the stage that performs a left outer join.
🔧 Debug
advanced
2: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?
Acustomer_id is stored as ObjectId, but query uses string 'abc123'
BThe orders collection is empty
CThe query syntax is invalid
DThe customer _id is incorrect
Attempts:
2 left
💡 Hint
Check the data types of the fields used in the query.
🧠 Conceptual
expert
3: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?
AStore customer ObjectId inside each order document and query orders by customer_id
BStore an array of all order ObjectIds inside the customer document
CEmbed all order documents inside the customer document
DDuplicate customer data inside each order document
Attempts:
2 left
💡 Hint
Consider document size limits and query efficiency.