0
0
MongoDBquery~20 mins

Array update with $[identifier] filtered in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Update Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Update specific array elements using $[identifier]

Given the following document in a collection products:

{
  _id: 1,
  name: "T-Shirt",
  sizes: [
    { size: "S", stock: 10 },
    { size: "M", stock: 5 },
    { size: "L", stock: 0 }
  ]
}

What will be the resulting document after running this update?

db.products.updateOne(
  { _id: 1 },
  { $inc: { "sizes.$[elem].stock": 3 } },
  { arrayFilters: [ { "elem.stock": { $lt: 5 } } ] }
)
A{ _id: 1, name: "T-Shirt", sizes: [ { size: "S", stock: 13 }, { size: "M", stock: 5 }, { size: "L", stock: 3 } ] }
B{ _id: 1, name: "T-Shirt", sizes: [ { size: "S", stock: 13 }, { size: "M", stock: 8 }, { size: "L", stock: 3 } ] }
C{ _id: 1, name: "T-Shirt", sizes: [ { size: "S", stock: 10 }, { size: "M", stock: 5 }, { size: "L", stock: 3 } ] }
D{ _id: 1, name: "T-Shirt", sizes: [ { size: "S", stock: 10 }, { size: "M", stock: 8 }, { size: "L", stock: 3 } ] }
Attempts:
2 left
💡 Hint

Look at which array elements have stock less than 5 before the update.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in array update with $[identifier]

Which of the following update commands will cause a syntax error when trying to update array elements with $[identifier]?

db.orders.updateOne(
  { _id: 101 },
  { $set: { "items.$[item].status": "shipped" } },
  { arrayFilters: [ { "item.qty": { $gte: 2 } } ] }
)
Adb.orders.updateOne({ _id: 101 }, { $set: { "items.$[item].status": "shipped" } }, { arrayFilters: [ { "item.qty": { $gte 2 } } ] })
Bdb.orders.updateOne({ _id: 101 }, { $set: { "items.$[item].status": "shipped" } }, { arrayFilters: [ { "item.qty": { $gte: 2 } } ] })
C)} ] } } 2 :etg$ { :"ytq.meti" { [ :sretliFyarra { ,} } "deppihs" :"sutats.]meti[$.smeti" { :tes$ { ,} 101 :di_ {(enOetadpu.sredro.bd
Db.orders.updateOne({ _id: 101 }, { $set: { "items.$[item].status": "shipped" } }, { arrayFilters: [ { "item.qty": { $gte: 2 } } ] })
Attempts:
2 left
💡 Hint

Check the syntax of the $gte operator in the array filter.

optimization
advanced
2:00remaining
Optimize update to increment multiple array elements conditionally

You want to increment the score by 1 for all players in the players array whose level is greater than 10.

Which update command is the most efficient and correct?

Adb.games.updateOne({ _id: 5 }, { $inc: { "players.$[p].score": 1 } })
Bdb.games.updateMany({}, { $inc: { "players.$[p].score": 1 } }, { arrayFilters: [ { "p.level": { $gt: 10 } } ] })
Cdb.games.updateOne({ _id: 5 }, { $inc: { "players.$.score": 1 } }, { arrayFilters: [ { "p.level": { $gt: 10 } } ] })
Ddb.games.updateOne({ _id: 5 }, { $inc: { "players.$[p].score": 1 } }, { arrayFilters: [ { "p.level": { $gt: 10 } } ] })
Attempts:
2 left
💡 Hint

Consider the filter on the document and the use of arrayFilters to target multiple array elements.

🔧 Debug
advanced
2:00remaining
Why does this array update not change any elements?

Given this document:

{ _id: 10, tasks: [ { name: "task1", done: false }, { name: "task2", done: false } ] }

And this update command:

db.projects.updateOne(
  { _id: 10 },
  { $set: { "tasks.$[t].done": true } },
  { arrayFilters: [ { "t.done": true } ] }
)

Why does this update not change any elements?

ABecause the positional operator $ cannot be used with arrayFilters.
BBecause the array filter matches elements where done is true, but all are false initially.
CBecause the update query is missing the filter on _id: 10.
DBecause the update operator $set cannot be used on array elements.
Attempts:
2 left
💡 Hint

Check the condition inside arrayFilters and the initial values.

🧠 Conceptual
expert
3:00remaining
Understanding multiple arrayFilters with different identifiers

Consider a document:

{
  _id: 20,
  orders: [
    { id: 1, items: [ { product: "A", qty: 2 }, { product: "B", qty: 1 } ] },
    { id: 2, items: [ { product: "A", qty: 1 }, { product: "C", qty: 5 } ] }
  ]
}

You want to increment the quantity by 1 for product "A" only in order with id 2.

Which update command achieves this?

A
db.sales.updateOne(
  { _id: 20 },
  { $inc: { "orders.$[order].items.$[item].qty": 1 } },
  { arrayFilters: [ { "order.id": 2 }, { "item.product": "A" } ] }
)
B
db.sales.updateOne(
  { _id: 20 },
  { $inc: { "orders.$[item].items.$[order].qty": 1 } },
  { arrayFilters: [ { "order.id": 2 }, { "item.product": "A" } ] }
)
C
db.sales.updateOne(
  { _id: 20 },
  { $inc: { "orders.$[order].items.$[item].qty": 1 } },
  { arrayFilters: [ { "item.id": 2 }, { "order.product": "A" } ] }
)
D
db.sales.updateOne(
  { _id: 20 },
  { $inc: { "orders.$.items.$[item].qty": 1 } },
  { arrayFilters: [ { "item.product": "A" } ] }
)
Attempts:
2 left
💡 Hint

Use two different identifiers in arrayFilters to target nested arrays.