0
0
MongoDBquery~10 mins

Array update with $[identifier] filtered in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array update with $[identifier] filtered
Start Update Command
Match Document
Identify Array Elements
Apply Filter $[identifier
Update Matched Array Elements
Save Document
Done
The update command finds the document, filters array elements using $[identifier], updates those elements, then saves the document.
Execution Sample
MongoDB
db.inventory.updateOne(
  { _id: 1 },
  { $set: { "items.$[elem].qty": 100 } },
  { arrayFilters: [ { "elem.status": "A" } ] }
)
This updates the qty to 100 for all items in the array with status 'A' in the document with _id 1.
Execution Table
StepActionDocument State BeforeArray Elements FilteredUpdate AppliedDocument State After
1Find document with _id:1{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }N/AN/A{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
2Filter array elements where status='A' using $[elem]{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }[{item:'apple', qty:5, status:'A'}]N/A{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
3Update qty to 100 for filtered elements{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }[{item:'apple', qty:5, status:'A'}]qty set to 100{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
4Save updated document{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }N/AN/A{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
5Update complete{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }N/AN/A{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
💡 All array elements with status 'A' updated; no other elements matched filter.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Document{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }{ _id:1, items:[{item:'apple', qty:5, status:'A'}, {item:'banana', qty:10, status:'B'} ] }{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }{ _id:1, items:[{item:'apple', qty:100, status:'A'}, {item:'banana', qty:10, status:'B'} ] }
Filtered Array Elements ($[elem])N/A[{item:'apple', qty:5, status:'A'}][{item:'apple', qty:100, status:'A'}][{item:'apple', qty:100, status:'A'}]
Key Moments - 2 Insights
Why does only the item with status 'A' get updated and not others?
Because the array filter $[elem] only matches elements where status='A' as shown in execution_table step 2, so only those elements are updated in step 3.
What happens if no array elements match the filter condition?
No elements get updated; the document remains unchanged as no array elements satisfy the filter, so update has no effect.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, which array elements are selected by the filter $[elem]?
ABoth apple and banana items
B[{item:'banana', qty:10, status:'B'}]
C[{item:'apple', qty:5, status:'A'}]
DNo elements selected
💡 Hint
Check the 'Array Elements Filtered' column at step 2 in execution_table.
At which step does the qty of the apple item change to 100?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Update Applied' and 'Document State After' columns in execution_table.
If the filter was changed to {"elem.status": "B"}, which item would be updated?
Abanana item
Bboth apple and banana items
Capple item
Dno items
💡 Hint
Refer to variable_tracker 'Filtered Array Elements' and how filter matches status.
Concept Snapshot
MongoDB array update with $[identifier]:
- Use $[identifier] in update path to target array elements.
- Define arrayFilters to specify which elements to update.
- Only elements matching filter get updated.
- Other elements remain unchanged.
- Saves document after update automatically.
Full Transcript
This visual execution shows how MongoDB updates array elements using the $[identifier] filtered syntax. First, the update command finds the document by _id. Then it filters array elements using the arrayFilters condition, selecting only those with status 'A'. Next, it updates the qty field of those filtered elements to 100. Finally, the updated document is saved. The execution table traces each step, showing document state before and after, which elements are filtered, and when the update happens. The variable tracker highlights how the document and filtered elements change. Key moments clarify why only matching elements update and what happens if no elements match. The quiz tests understanding of filtering and update timing. The snapshot summarizes the syntax and behavior for quick reference.