0
0
MongoDBquery~10 mins

Array update with positional $ operator 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 update the first matching element in the array using the positional $ operator.

MongoDB
db.collection.updateOne({ 'items.name': 'apple' }, { $set: { 'items.[1].quantity': 10 } })
Drag options to blanks, or click blank then click option'
A$
B0
C1
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using a numeric index instead of the positional operator.
Using an asterisk or other symbols instead of $.
2fill in blank
medium

Complete the code to increment the quantity of the matched array element by 5 using the positional $ operator.

MongoDB
db.collection.updateOne({ 'items.name': 'banana' }, { $inc: { 'items.[1].quantity': 5 } })
Drag options to blanks, or click blank then click option'
A1
B0
C$
D#
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 or 1 instead of the positional operator.
Using invalid symbols like #.
3fill in blank
hard

Fix the error in the update query by correctly using the positional $ operator to set the price of the matched item.

MongoDB
db.collection.updateOne({ 'products.code': 'X123' }, { $set: { 'products.[1].price': 20 } })
Drag options to blanks, or click blank then click option'
A0
B$
C1
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric indices instead of the positional operator.
Using invalid symbols like *.
4fill in blank
hard

Fill both blanks to update the quantity and price of the matched array element using the positional $ operator.

MongoDB
db.collection.updateOne({ 'orders.id': 101 }, { $set: { 'orders.[1].quantity': 15, 'orders.[2].price': 30 } })
Drag options to blanks, or click blank then click option'
A$
B0
C1
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for quantity and price fields.
Using invalid symbols instead of $.
5fill in blank
hard

Fill all three blanks to update the quantity, price, and status of the matched array element using the positional $ operator.

MongoDB
db.collection.updateOne({ 'inventory.sku': 'A1B2' }, { $set: { 'inventory.[1].quantity': 50, 'inventory.[2].price': 99.99, 'inventory.[3].status': 'in stock' } })
Drag options to blanks, or click blank then click option'
A$
B0
C1
D#
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric indices instead of the positional operator.
Mixing different operators in the same update.