Bird
Raised Fist0
DynamoDBquery~10 mins

Multiple actions in one update 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 - Multiple actions in one update
Start Update Request
Parse Update Expression
Identify Actions: SET, REMOVE, ADD, DELETE
Apply SET actions
Apply REMOVE actions
Apply ADD actions
Apply DELETE actions
Save Updated Item
Return Updated Attributes
The update request is parsed to identify multiple actions, which are applied in order: SET, REMOVE, ADD, DELETE, then the item is saved and updated attributes returned.
Execution Sample
DynamoDB
UpdateItem {
  Key: {id: "123"},
  UpdateExpression: "SET age = age + :inc REMOVE nickname ADD points :pts",
  ExpressionAttributeValues: {":inc": 1, ":pts": 10}
}
This update increases age by 1, removes nickname attribute, and adds 10 to points in one request.
Execution Table
StepAction TypeAttributeOperationValueResulting Item State
1Parse Update Expression---{"id":"123", "age": 30, "nickname": "Sam", "points": 50}
2SETageage + :inc1{"id":"123", "age": 31, "nickname": "Sam", "points": 50}
3REMOVEnicknameremove attribute-{"id":"123", "age": 31, "points": 50}
4ADDpointspoints + :pts10{"id":"123", "age": 31, "points": 60}
5Save Item---{"id":"123", "age": 31, "points": 60}
6Return Updated Attributes---{"age": 31, "points": 60}
💡 All update actions applied; item saved with new attribute values.
Variable Tracker
VariableStartAfter SETAfter REMOVEAfter ADDFinal
age3031313131
nickname"Sam""Sam"nullnullnull
points5050506060
Key Moments - 3 Insights
Why does the REMOVE action delete the nickname attribute?
Because in the execution_table row 3, REMOVE action explicitly removes the nickname attribute, changing its value to null (absent) in the item state.
How does the ADD action affect the points attribute?
As shown in execution_table row 4, ADD increases points by the value 10, changing points from 50 to 60.
Why is the SET action applied before REMOVE and ADD?
The update expression actions are applied in a fixed order: SET first, then REMOVE, then ADD, ensuring predictable updates as seen in the execution_table steps 2, 3, and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of age after the SET action?
A31
B30
C32
Dnull
💡 Hint
Check the 'After SET' column in variable_tracker and row 2 in execution_table.
At which step is the nickname attribute removed from the item?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action Type' and 'Attribute' columns in execution_table.
If the ADD action was omitted, what would be the final value of points?
A60
B10
C50
Dnull
💡 Hint
Refer to variable_tracker and note points value before and after ADD action.
Concept Snapshot
UpdateItem in DynamoDB can perform multiple actions in one request.
Actions include SET (change or add attributes), REMOVE (delete attributes), ADD (increment numbers or add to sets), DELETE (remove from sets).
Actions are applied in order: SET, REMOVE, ADD, DELETE.
Use ExpressionAttributeValues for dynamic values.
The updated item is saved and optionally returned.
Full Transcript
This visual execution shows how DynamoDB processes multiple actions in one UpdateItem request. First, the update expression is parsed to identify actions: SET, REMOVE, ADD, and DELETE. Then, each action is applied in order. For example, SET updates or adds attributes, REMOVE deletes attributes, ADD increments numeric attributes or adds to sets, and DELETE removes from sets. The item state changes step-by-step, reflecting each action. Finally, the updated item is saved and the requested attributes are returned. This process ensures multiple changes happen atomically in one update call.

Practice

(1/5)
1. In DynamoDB, which update action allows you to change or add new attributes to an item in a single update request?
easy
A. REMOVE
B. SET
C. ADD
D. DELETE

Solution

  1. Step 1: Understand update actions in DynamoDB

    DynamoDB update expressions use SET to add or modify attributes in an item.
  2. Step 2: Identify the correct action for adding or changing attributes

    SET is used to add new attributes or change existing ones in a single update request.
  3. Final Answer:

    SET -> Option B
  4. Quick Check:

    SET updates attributes [OK]
Hint: Use SET to add or change attributes in one update [OK]
Common Mistakes:
  • Confusing REMOVE with adding attributes
  • Using ADD to change non-numeric attributes
  • Thinking DELETE removes attributes instead of set elements
2. Which of the following is the correct syntax to update an attribute score by adding 10 in a DynamoDB update expression?
easy
A. DELETE score 10
B. SET score = score + 10
C. REMOVE score + 10
D. ADD score 10

Solution

  1. Step 1: Recall syntax for numeric increments in DynamoDB

    To increase a numeric attribute, DynamoDB uses ADD followed by the attribute and the value.
  2. Step 2: Match the correct syntax

    ADD score 10 correctly adds 10 to the existing score attribute.
  3. Final Answer:

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

    ADD increments numbers [OK]
Hint: Use ADD to increase numeric attributes in update [OK]
Common Mistakes:
  • Using SET with arithmetic expressions (not supported)
  • Using REMOVE or DELETE for numeric increments
  • Incorrect syntax like missing commas or keywords
3. Given the following update expression:
SET age = age + :inc REMOVE nickname ADD points :pts DELETE tags :oldTags
What will happen if the item initially has age = 30, nickname = 'Sam', points = 5, and tags = {'red', 'blue'} with :inc = 2, :pts = 3, and :oldTags = {'blue'}?
medium
A. age becomes 2, nickname removed, points become 8, tags become {'red', 'blue'}
B. age becomes 32, nickname removed, points become 3, tags become {'red', 'blue'}
C. age becomes 32, nickname removed, points become 8, tags become {'red'}
D. age becomes 32, nickname unchanged, points become 8, tags become {'red'}

Solution

  1. Step 1: Apply each update action to the initial item

    SET age = age + :inc adds 2 to 30 -> 32; REMOVE nickname deletes 'Sam'; ADD points :pts adds 3 to 5 -> 8; DELETE tags :oldTags removes 'blue' from {'red', 'blue'} -> {'red'}.
  2. Step 2: Confirm final attribute values

    age=32, nickname removed, points=8, tags={'red'} matches age becomes 32, nickname removed, points become 8, tags become {'red'}.
  3. Final Answer:

    age becomes 32, nickname removed, points become 8, tags become {'red'} -> Option C
  4. Quick Check:

    Multiple actions update all attributes correctly [OK]
Hint: Apply each action step-by-step to see final values [OK]
Common Mistakes:
  • Forgetting REMOVE deletes attribute
  • Confusing DELETE with REMOVE for sets
  • Misapplying ADD to sets instead of numbers
4. Identify the error in this DynamoDB update expression:
SET name = :n ADD age :a REMOVE points, DELETE tags :t
medium
A. Comma after REMOVE points is invalid syntax
B. ADD cannot be used with a non-numeric attribute like age
C. SET cannot assign a value using a placeholder like :n
D. REMOVE and DELETE cannot be used together in one update

Solution

  1. Step 1: Check syntax for multiple update actions

    REMOVE action lists attributes separated by spaces, not commas. The comma after points is invalid.
  2. Step 2: Validate other parts

    ADD can be used with numeric attributes; SET can use placeholders; REMOVE and DELETE can coexist.
  3. Final Answer:

    Comma after REMOVE points is invalid syntax -> Option A
  4. Quick Check:

    REMOVE uses spaces, not commas [OK]
Hint: Use spaces, not commas, to separate REMOVE attributes [OK]
Common Mistakes:
  • Using commas in REMOVE or DELETE lists
  • Assuming ADD works on non-numeric attributes
  • Thinking placeholders are not allowed in SET
5. You want to update a DynamoDB item to do all these in one update:
- Increase stock by 5
- Remove attribute discontinued
- Add newTag to a set attribute tags
Which update expression correctly performs all these actions?
hard
A. ADD stock :inc REMOVE discontinued ADD tags :newTag
B. SET stock = stock + :inc REMOVE discontinued ADD tags :newTag
C. ADD stock :inc REMOVE discontinued SET tags = tags + :newTag
D. SET stock = stock + :inc DELETE discontinued ADD tags :newTag

Solution

  1. Step 1: Choose correct action for increasing numeric attribute

    ADD stock :inc correctly increases numeric stock by :inc.
  2. Step 2: Remove attribute and add to set correctly

    REMOVE discontinued deletes the attribute; ADD tags :newTag adds elements to the set.
  3. Step 3: Verify syntax correctness

    ADD stock :inc REMOVE discontinued ADD tags :newTag uses ADD for numeric and set additions, REMOVE for attribute removal, all valid.
  4. Final Answer:

    ADD stock :inc REMOVE discontinued ADD tags :newTag -> Option A
  5. Quick Check:

    Use ADD for numbers and sets, REMOVE for attributes [OK]
Hint: Use ADD for numbers and sets, REMOVE for attributes [OK]
Common Mistakes:
  • Using SET with arithmetic expressions (not supported)
  • Using DELETE instead of REMOVE for attributes
  • Trying to add to sets with SET instead of ADD