Perform DynamoDB Conditional Update (Using a Visual Editor)

Written by Rafal Wilinski
Published on May 25th, 2021
Time to 10x your DynamoDB productivity with Dynobase [learn more]
DynamoDB lacks an equivalent of SQL-ish UPDATE X WHERE Y
. Despite supporting PartiQL, a query language very similar to SQL, that operation is still not possible because of how DynamoDB works - you cannot update multiple rows at once, you can only run putItem / updateItem on a per item basis.
Does it mean that we cannot update a collection of items conditionally in DynamoDB?
No! We can do it, it's just a bit more complicated. Here's what we need to do:
- First, we need to fetch a collection of items that we'd like to update.
- Then, run an update operation on each returned item.
If you were to write this operation on your own, it can quickly grow to over 100 lines of code if you'd like to handle pagination and all the cases gracefully.
Fortunately, that is also possible in Dynobase and it's much easier. For example:
Let's suppose we'd like to update every record in our database where ID has only two characters and prefix it with
updated_
- an example ID89
will becomeupdated_89
.
In SQL, you'd probably write something like this:
Let's recreate it in DynamoDB! First, you need to Download Dynobase.
Next, open it, select profile, region and table of your choice and click "Terminal", right next to the "Scan" / "Query" button, a small code editor will popup.
Inside this Editor, paste following piece of code:
and then run "Execute" - and voilà !
You dataset has been updated! Keep in mind that because Dynobase updates each record one-by-one under the hood, it can take quite long on larger datasets. You can also use the query filters to make sure that your update will only affect relevant piece of data.