0
0
MongoDBquery~10 mins

Schema design for read-heavy workloads in MongoDB - Interactive Code Practice

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

Complete the code to create a collection optimized for fast reads by embedding related data.

MongoDB
db.users.insertOne({ name: "Alice", orders: [1] })
Drag options to blanks, or click blank then click option'
Anull
B[{ item: "book", qty: 2 }, { item: "pen", qty: 5 }]
CObjectId("507f1f77bcf86cd799439011")
D"orders"
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing orders by ID instead of embedding causes slower reads.
Using null or unrelated strings does not store order data.
2fill in blank
medium

Complete the query to find users with embedded orders containing item 'book'.

MongoDB
db.users.find({ "orders.item": [1] })
Drag options to blanks, or click blank then click option'
A"notebook"
B"pen"
C"book"
D"pencil"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong item name returns no results.
Querying on the wrong field does not find embedded documents.
3fill in blank
hard

Fix the error in the update to add a new embedded order to user 'Alice'.

MongoDB
db.users.updateOne({ name: "Alice" }, { [1]: { orders: { item: "notebook", qty: 3 } } })
Drag options to blanks, or click blank then click option'
A$push
B$set
C$addToSet
D$inc
Attempts:
3 left
💡 Hint
Common Mistakes
Using $set replaces the entire orders array instead of adding.
Using $inc is for numbers, not arrays.
4fill in blank
hard

Fill both blanks to create an index on the embedded field for faster reads.

MongoDB
db.users.createIndex({ [1]: [2] })
Drag options to blanks, or click blank then click option'
Aorders.item
B1
C-1
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Indexing the wrong field does not improve query speed.
Using -1 creates descending index, which is less common here.
5fill in blank
hard

Fill all three blanks to write an aggregation pipeline that unwinds orders and filters by qty > 2.

MongoDB
db.users.aggregate([ { $unwind: "$[1]" }, { $match: { "[2].qty": { [3]: 2 } } } ])
Drag options to blanks, or click blank then click option'
Aorders
C$gt
D$lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt filters for less than, which is incorrect here.
Not unwinding the array prevents filtering individual orders.