0
0
DynamoDBquery~10 mins

ADD expression for numeric increment in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ADD expression for numeric increment
Start with item in table
Prepare UpdateExpression with ADD
Send UpdateItem request
DynamoDB adds number to attribute
Return updated item or success
End
The flow shows how DynamoDB processes an ADD expression to increase a numeric attribute by a given value.
Execution Sample
DynamoDB
UpdateItem {
  Key: { id: '123' },
  UpdateExpression: 'ADD counter :inc',
  ExpressionAttributeValues: { ':inc': 1 }
}
This code increments the 'counter' attribute of the item with id '123' by 1.
Execution Table
StepActionAttribute 'counter' Value BeforeADD ValueAttribute 'counter' Value AfterNotes
1Read item with id '123'5-5Initial value is 5
2Prepare UpdateExpression 'ADD counter :inc'51-Ready to add 1
3Send UpdateItem request51-Request sent to DynamoDB
4DynamoDB adds 1 to 'counter'516New value is 6
5Return success with updated value6-6Update complete
💡 Update completes after adding 1 to the existing 'counter' value.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
counter5566
:inc1111
Key Moments - 2 Insights
Why does the 'counter' value change from 5 to 6 after the ADD expression?
Because the ADD expression tells DynamoDB to add the value of ':inc' (which is 1) to the current 'counter' value (5), resulting in 6 as shown in execution_table row 4.
What happens if the 'counter' attribute does not exist before the ADD operation?
DynamoDB treats the missing attribute as zero and adds the given number, so the attribute will be created with the added value. This is implied by the ADD behavior in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'counter' after Step 3?
A6
B5
C1
DUndefined
💡 Hint
Check the 'Attribute 'counter' Value After' column for Step 3 in the execution_table.
At which step does DynamoDB actually perform the addition?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where 'DynamoDB adds 1 to counter' in the execution_table.
If ':inc' was 3 instead of 1, what would be the final 'counter' value?
A5
B6
C8
D3
💡 Hint
Refer to variable_tracker and execution_table logic for adding ':inc' to 'counter'.
Concept Snapshot
DynamoDB ADD expression increments a numeric attribute.
Syntax: UpdateExpression: 'ADD attribute :value'
If attribute missing, treated as 0 before adding.
Use ExpressionAttributeValues to specify increment.
Result updates attribute atomically.
Full Transcript
This visual execution trace shows how DynamoDB's ADD expression works to increment a numeric attribute. Starting with an item having a 'counter' value of 5, the UpdateExpression 'ADD counter :inc' with ':inc' equal to 1 is prepared. When the UpdateItem request is sent, DynamoDB adds 1 to the existing 'counter' value, resulting in 6. The updated value is then returned. The variable tracker confirms the 'counter' changes from 5 to 6 after the addition. Key moments clarify that the ADD operation adds the increment to the current value and creates the attribute if missing. The quiz questions test understanding of the step when addition happens, the value after sending the request, and the effect of changing the increment value.