Bird
Raised Fist0
DynamoDBquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the ADD expression do in DynamoDB when updating a numeric attribute?
easy
A. It increases or decreases the numeric attribute by a specified value without reading the current value first.
B. It replaces the numeric attribute with a new value directly.
C. It deletes the numeric attribute from the item.
D. It multiplies the numeric attribute by a specified value.

Solution

  1. Step 1: Understand the ADD expression purpose

    The ADD expression in DynamoDB is used to increment or decrement numeric attributes by adding a value.
  2. Step 2: Compare with other operations

    Unlike SET which replaces values, ADD modifies the existing numeric value by adding to it without reading it first.
  3. Final Answer:

    It increases or decreases the numeric attribute by a specified value without reading the current value first. -> Option A
  4. Quick Check:

    ADD expression updates numeric attributes by adding value [OK]
Hint: ADD changes numbers by adding without reading current value [OK]
Common Mistakes:
  • Thinking ADD replaces the value instead of adding
  • Confusing ADD with DELETE operation
  • Assuming ADD multiplies the number
2. Which of the following is the correct syntax to increment a numeric attribute score by 5 using the ADD expression in DynamoDB UpdateExpression?
easy
A. SET score ADD 5
B. SET score = score + 5
C. ADD score '5'
D. ADD score :inc

Solution

  1. Step 1: Recall DynamoDB UpdateExpression syntax for ADD

    The ADD expression requires the attribute name followed by a placeholder for the value, like ADD score :inc.
  2. Step 2: Check other options for syntax errors

    SET score = score + 5 uses SET with arithmetic which is invalid. ADD score '5' uses a string literal which is invalid for ADD. SET score ADD 5 mixes SET and ADD incorrectly.
  3. Final Answer:

    ADD score :inc -> Option D
  4. Quick Check:

    Correct ADD syntax uses attribute and value placeholder [OK]
Hint: Use ADD attribute :value with placeholder for increment [OK]
Common Mistakes:
  • Using SET with arithmetic instead of ADD
  • Using a string literal instead of numeric placeholder
  • Mixing SET and ADD keywords incorrectly
3. Given the following DynamoDB update code snippet:
UpdateExpression: "ADD inventory :inc",
ExpressionAttributeValues: {":inc": 3}

If the current inventory value is 7, what will be the new value after the update?
medium
A. 3
B. Error: Cannot add without reading value
C. 10
D. 7

Solution

  1. Step 1: Understand ADD increments current value

    The ADD expression adds the given value (3) to the existing attribute value (7).
  2. Step 2: Calculate the new value

    7 + 3 = 10, so the new inventory value will be 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    7 + 3 = 10 after ADD update [OK]
Hint: ADD adds value to current number, no read needed [OK]
Common Mistakes:
  • Assuming ADD replaces value instead of adding
  • Thinking ADD requires reading current value first
  • Choosing the original value instead of updated
4. You wrote this DynamoDB update:
UpdateExpression: "ADD count :val",
ExpressionAttributeValues: {":val": "2"}

But it fails with a validation error. What is the most likely cause?
medium
A. The attribute name 'count' is reserved and cannot be used.
B. The value for :val is a string instead of a number.
C. ADD expression cannot be used with numeric attributes.
D. ExpressionAttributeValues must not use colons in keys.

Solution

  1. Step 1: Check the type of ExpressionAttributeValues

    The value for :val is given as a string "2" instead of a numeric type.
  2. Step 2: Understand ADD requires numeric values

    ADD expression only works with numbers, so passing a string causes a validation error.
  3. Final Answer:

    The value for :val is a string instead of a number. -> Option B
  4. Quick Check:

    ADD needs numeric value, not string [OK]
Hint: Ensure increment value is numeric, not string [OK]
Common Mistakes:
  • Passing increment as string instead of number
  • Misunderstanding reserved words error
  • Incorrectly formatting ExpressionAttributeValues keys
5. You want to decrement a user's loginAttempts count by 1 using DynamoDB's ADD expression. Which UpdateExpression and ExpressionAttributeValues correctly achieve this?
hard
A. UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": -1}
B. UpdateExpression: "ADD loginAttempts '-1'", ExpressionAttributeValues: {}
C. UpdateExpression: "SET loginAttempts = loginAttempts - 1", ExpressionAttributeValues: {}
D. UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": 1}

Solution

  1. Step 1: Use ADD with a negative number to decrement

    To decrease a numeric attribute, ADD accepts a negative value as the increment.
  2. Step 2: Check syntax correctness

    UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": -1} correctly uses a placeholder :dec with value -1, which is valid syntax.
  3. Step 3: Eliminate incorrect options

    UpdateExpression: "ADD loginAttempts '-1'", ExpressionAttributeValues: {} uses a string literal '-1' which is invalid for ADD. UpdateExpression: "SET loginAttempts = loginAttempts - 1", ExpressionAttributeValues: {} uses SET with arithmetic which is not allowed. UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": 1} adds +1, which increments instead of decrementing.
  4. Final Answer:

    UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": -1} -> Option A
  5. Quick Check:

    Use ADD with negative value to decrement [OK]
Hint: Use ADD with negative number placeholder to decrement [OK]
Common Mistakes:
  • Trying to use SET with arithmetic expressions
  • Using string literal for decrement value
  • Using positive number to decrement