0
0
DynamoDBquery~5 mins

ADD expression for numeric increment in DynamoDB

Choose your learning style9 modes available
Introduction
The ADD expression lets you increase or decrease a number in your database easily without replacing the whole item.
When you want to count how many times something happens, like page views.
When you need to add points to a user's score in a game.
When you want to update inventory counts after a sale.
When you want to track how many times a button is clicked.
When you want to increase a numeric value without reading it first.
Syntax
DynamoDB
UpdateExpression: "ADD attributeName :incrementValue"
ExpressionAttributeValues: { ":incrementValue": {"N": "number"} }
The ADD expression only works with numbers or sets in DynamoDB.
You provide the amount to add as a value with a colon prefix, like :incrementValue.
Examples
This adds 1 to the 'visits' attribute.
DynamoDB
UpdateExpression: "ADD visits :inc"
ExpressionAttributeValues: { ":inc": {"N": "1"} }
This adds 10 to the 'score' attribute.
DynamoDB
UpdateExpression: "ADD score :points"
ExpressionAttributeValues: { ":points": {"N": "10"} }
This subtracts 5 from the 'stock' attribute (using a negative number).
DynamoDB
UpdateExpression: "ADD stock :amount"
ExpressionAttributeValues: { ":amount": {"N": "-5"} }
Sample Program
This command adds 3 to the Quantity attribute of the product with ProductId '123'. It returns the new Quantity value.
DynamoDB
aws dynamodb update-item \
  --table-name Products \
  --key '{"ProductId": {"S": "123"}}' \
  --update-expression "ADD Quantity :inc" \
  --expression-attribute-values '{":inc": {"N": "3"}}' \
  --return-values UPDATED_NEW
OutputSuccess
Important Notes
If the attribute does not exist, ADD creates it with the value you provide.
You can use negative numbers to subtract values.
ADD works only with number or set types, not strings.
Summary
ADD expression updates numeric attributes by adding a value.
It can increase or decrease numbers without reading the current value first.
Useful for counters, scores, and inventory updates.