Member-only story
Increment Number Values In DynamoDB With The ADD Operator
Easily increment number counters in your DynamoDB tables with this method
Have you ever needed to increment a number value in DynamoDB but weren’t sure how to?
I’ve seen many users fetch the previous value of that counter just to be able to increment it.
This method, while it works, isn’t efficient. It is costing you an extra read each time you need to update the item’s value.
There is however a DynamoDB API method that will allow you to increment number counters without knowing what the previous value was.
Let’s take a look at this.
The ADD Operator
The ADD operator in DynamoDB allows you to update an item’s number value.
This is typically done in an update expression using the ADD operator in the UpdateExpression of your UpdateCommand.
Here’s a code example:
UpdateExpression: 'ADD #count :incr',
ExpressionAttributeNames: {
'#count': 'count',
},
ExpressionAttributeValues: {
':incr': 1,
},The ADD operator is defined in the UpdateExpression.








