I have a use case where I need to store how many people view the blog post in DynamoDB.
Lets' say I have a table like below
blogId | Total Views. |
---|---|
1 | 100 |
Now when a user views a new blog with ID 2
I have to store that in the table. I can do the following to achieve that:
isBlogExists = dynamodbMapper.get(BlogViews,{blogId: 2})
if (isBlogExists){
dynamodbMapper.set("totalViews", new MathematicalExpression("totalViews", "+", 1))
} else{
dynamodbMapper.set("totalViews", 1)
}
Unfortunately it ends up in race condition so I planned to use the SET
operation like this
dynamodbMapper.set("totalViews", new MathematicalExpression("totalViews", "+", 1))
Since this item doesn't have the totalViews attribute, I am getting a error like
No item with the key found in Table.
So does DynamoDB doesn't support incrementing numeric values on key which doesn't exist?
I could use the ADD
operation but DynamoDB recommends to use SET
over ADD
(source).