0
0
MongoDBquery~10 mins

Joins vs embedding decision in MongoDB - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to embed an address inside a user document in MongoDB.

MongoDB
db.users.insertOne({ name: "Alice", address: [1] })
Drag options to blanks, or click blank then click option'
AObjectId("507f1f77bcf86cd799439011")
B[ "123 Main St", "Townsville" ]
C{ street: "123 Main St", city: "Townsville" }
D"123 Main St, Townsville"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array instead of an object for the address.
Using a string instead of a structured object.
Referencing an ObjectId instead of embedding the data.
2fill in blank
medium

Complete the code to perform a $lookup join between orders and customers collections.

MongoDB
db.orders.aggregate([{ $lookup: { from: "[1]", localField: "customerId", foreignField: "_id", as: "customerInfo" } }])
Drag options to blanks, or click blank then click option'
Aproducts
Baddresses
Corders
Dcustomers
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong collection name in the 'from' field.
Joining with the same collection instead of the related one.
3fill in blank
hard

Fix the error in the query to embed multiple phone numbers inside a user document.

MongoDB
db.users.insertOne({ name: "Bob", phones: [1] })
Drag options to blanks, or click blank then click option'
A[ { type: "home", number: "123-4567" }, { type: "work", number: "987-6543" } ]
B{ home: "123-4567", work: "987-6543" }
C"123-4567, 987-6543"
DObjectId("507f1f77bcf86cd799439012")
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single object instead of an array for multiple phones.
Using a string or ObjectId instead of structured data.
4fill in blank
hard

Fill both blanks to write a $lookup that joins orders with products and filters only orders with quantity greater than 5.

MongoDB
db.orders.aggregate([{ $lookup: { from: "[1]", localField: "productId", foreignField: "_id", as: "productDetails" } }, { $match: { quantity: { [2]: 5 } } }])
Drag options to blanks, or click blank then click option'
Aproducts
B$gt
C$lt
Dcustomers
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong collection name in $lookup.
Using $lt instead of $gt for filtering.
5fill in blank
hard

Fill all three blanks to create a query that embeds a review inside a product document only if the rating is 4 or higher.

MongoDB
db.products.updateOne({ _id: [1] }, { $push: { reviews: { userId: [2], rating: [3] } } })
Drag options to blanks, or click blank then click option'
AObjectId("507f1f77bcf86cd799439013")
BObjectId("507f1f77bcf86cd799439014")
C4
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of ObjectId for IDs.
Using a rating less than 4.