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
Recall & Review
beginner
What does the ADD expression do in DynamoDB?
The ADD expression increases (or decreases) the value of a numeric attribute by a specified amount. It can also add elements to a set attribute.
Click to reveal answer
beginner
How do you increment a numeric attribute named 'counter' by 1 using the ADD expression?
Use the UpdateExpression: ADD counter :inc where :inc is a value of 1 in ExpressionAttributeValues.
Click to reveal answer
intermediate
Can the ADD expression be used to decrement a numeric attribute in DynamoDB?
Yes, by adding a negative number. For example, ADD counter :dec where :dec is -1 will decrement the counter by 1.
Click to reveal answer
intermediate
What happens if the attribute does not exist when you use ADD to increment it?
DynamoDB creates the attribute and sets it to the increment value. For example, if 'counter' does not exist and you ADD 1, 'counter' becomes 1.
Click to reveal answer
advanced
Is the ADD expression atomic in DynamoDB updates?
Yes, the ADD operation is atomic, meaning it safely increments or decrements the attribute even if multiple updates happen at the same time.
Click to reveal answer
Which DynamoDB UpdateExpression syntax increments a numeric attribute 'score' by 5?
AADD score :val
BSET score = score + 5
CADD score 5
DSET score + 5
✗ Incorrect
The correct syntax uses ADD with a placeholder value, e.g., ADD score :val where :val is 5.
What value should you provide to decrement a numeric attribute by 3 using ADD?
A0
B3
C-3
DCannot decrement with ADD
✗ Incorrect
You provide a negative number, so -3 will decrement the attribute by 3.
If the attribute 'visits' does not exist, what will ADD visits :inc with :inc = 1 do?
ACreate 'visits' with value 1
BReturn an error
CIgnore the update
DSet 'visits' to 0
✗ Incorrect
DynamoDB creates the attribute and sets it to the increment value.
Is the ADD operation in DynamoDB atomic?
AOnly when used with SET
BNo, it can cause conflicts
COnly for string attributes
DYes, it prevents race conditions
✗ Incorrect
ADD is atomic, so concurrent updates safely increment or decrement the attribute.
Which data types can the ADD expression modify in DynamoDB?
AOnly numeric attributes
BNumeric attributes and sets
COnly string attributes
DAll attribute types
✗ Incorrect
ADD can increment numeric attributes and add elements to set attributes.
Explain how the ADD expression works for incrementing a numeric attribute in DynamoDB.
Think about how you tell DynamoDB to add a number to an attribute safely.
You got /4 concepts.
Describe how you would decrement a numeric attribute using the ADD expression in DynamoDB.
Remember ADD can add negative values to subtract.
You got /3 concepts.
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
Step 1: Understand the ADD expression purpose
The ADD expression in DynamoDB is used to increment or decrement numeric attributes by adding a value.
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.
Final Answer:
It increases or decreases the numeric attribute by a specified value without reading the current value first. -> Option A
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
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.
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.
Final Answer:
ADD score :inc -> Option D
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:
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
Step 1: Use ADD with a negative number to decrement
To decrease a numeric attribute, ADD accepts a negative value as the increment.
Step 2: Check syntax correctness
UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": -1} correctly uses a placeholder :dec with value -1, which is valid syntax.
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.
Final Answer:
UpdateExpression: "ADD loginAttempts :dec", ExpressionAttributeValues: {":dec": -1} -> Option A
Quick Check:
Use ADD with negative value to decrement [OK]
Hint: Use ADD with negative number placeholder to decrement [OK]