0
0
MongoDBquery~10 mins

Array update with positional $ operator in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array update with positional $ operator
Find document matching query
Locate array element matching condition
Use positional $ operator to target element
Update the targeted array element
Save document
This flow shows how MongoDB finds a document, locates the matching array element using the positional $ operator, updates it, and saves the document.
Execution Sample
MongoDB
db.students.updateOne(
  { "grades.grade": 85 },
  { $set: { "grades.$.grade": 90 } }
)
This updates the first array element in 'grades' with grade 85 to have grade 90.
Execution Table
StepQuery ConditionArray Element CheckedMatch Found?Update ActionDocument State After Step
1{"grades.grade": 85}grades[0] = {grade: 80}NoNo update{"grades": [{"grade": 80}, {"grade": 85}, {"grade": 88}]}
2{"grades.grade": 85}grades[1] = {grade: 85}YesUpdate grades[1].grade to 90{"grades": [{"grade": 80}, {"grade": 90}, {"grade": 88}]}
3N/AN/AN/AUpdate complete{"grades": [{"grade": 80}, {"grade": 90}, {"grade": 88}]}
💡 Update stops after first matching array element is updated using positional $ operator.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Document grades array[{grade:80}, {grade:85}, {grade:88}]Same[{grade:80}, {grade:90}, {grade:88}][{grade:80}, {grade:90}, {grade:88}]
Matched array indexNoneNone11
Key Moments - 3 Insights
Why does only the first matching array element get updated?
Because the positional $ operator targets only the first array element that matches the query condition, as shown in execution_table row 2.
What happens if no array element matches the condition?
No update occurs; the document remains unchanged, as shown in execution_table row 1 where no match is found.
Can the positional $ operator update multiple elements at once?
No, it updates only the first matching element. To update multiple elements, other operators like $[] are needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of grades[1].grade after Step 2?
A85
B80
C90
D88
💡 Hint
Check the 'Document State After Step' column in row 2.
At which step does the positional $ operator apply the update?
AStep 2
BStep 1
CStep 3
DNo update applied
💡 Hint
Look at the 'Update Action' column to see when the update happens.
If the query was {"grades.grade": 100}, what would happen?
Agrades[1].grade updates to 90
BNo array element matches, no update
CAll grades updated to 90
DError occurs
💡 Hint
Refer to key_moments about no matching element scenario.
Concept Snapshot
MongoDB positional $ operator updates the first array element matching the query.
Syntax: { $set: { "arrayField.$.field": value } }
Only the first matching element is updated.
If no match, no update occurs.
Use for precise array element updates inside documents.
Full Transcript
This visual execution shows how MongoDB updates an array element using the positional $ operator. First, it finds the document with an array element matching the query condition. Then it locates the first matching element in the array. Using the positional $ operator, it updates that element's field. The update stops after the first match. If no element matches, no update happens. This operator is useful for updating one specific array element inside a document.