'Dynamodb update based on other field

Currently I have a posts table which stores likes and popularity, basically I want to change the popularity field depending on this likes field, such as:

if (likes > 100) popularity = 3
else if (likes > 50) popularity = 2
else if (likes > 10) popularity = 1
else popularity = 0

Popularity(0) means the post is new, whereas popularity(3) posts are the ones users see the most on their explore page.

Right now I need to fetch each post to get their total likes and then call an update for its popularity. Is there a way to do this in one single update function without fetching the post beforehand?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source