Inserting items and modifying attributes and nested attributes with update in DynamoDB

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

The update function works differently than put does. Whereas put will insert-or-overwrite, update will update-or-insert. We cover what you can do with an UpdateExpression, which turns out to be a lot.

Instructor: [00:00] In DynamoDB, the update command is a lot like the put command. Except where put will insert or overwrite an item, update will update or insert an item. That means that put will first try to insert and secondly, replace a specific item, while update will first try to modify and secondly, try to insert if the item does not exist.

[00:23] On the left, we can see that our table has no items in it. On the right, we've got some params and a call to update. Note that we're operating on the key user one, todo one with an update expression. An update expression can contain a number of different clauses.

[00:38] Set is the one you'll be using most often. Note that we're also using expression attribute names and expression attribute values to handle the replacement of various values in our set expression.

[00:48] In this case, we're replacing the attribute name because text is a reserved word and the value because typically, this will be a dynamic data coming in from the user, not static data that we have in a test here. If we run the update, we can refresh the page and see that we're supposed to get ice cream. Update has inserted a new item into the table.

[01:07] Note that now we've changed the update expression to specify a data attribute that equals a new object. Note that if we were using put here, this call would overwrite the entire item. Because we're using update, we see the PK then the SK of the item, which haven't changed the data attribute we just added.

[01:24] If we scroll right, we'd see the text, "Forget ice cream." Note that done is currently set to true and done is a nested field on the data attribute. We've updated our update expression to set two different values. You can include as many as you want.

[01:38] In this case, we're setting the text attribute to the text value, which will update get ice cream to do something. You'll notice that we've also set a nested field long attribute. Because data is a map, we can set done inside of data and we'll set it to false.

[01:51] Note that we can't use the key word false to be a value here. We'll replace it with done and rerun our query. Now that we've succeeded, we can see that user one todo one has a done value of false and a text value of do something. We've updated multiple attributes, including a nested field inside of an attribute map in the same call.

[02:10] Before we explore the other applications of set, note that we can use return values, return ConsumedCapacity and condition expression just like we might do in a put. Update expressions include a number of clauses that we can use, including set, remove, add and delete.

[02:26] In the documentation, it says that you should prefer set over add, so we won't be covering add here. Also note that set can do quite a few things, so it'll be the clause that you're using the most.

[02:36] To recap, set can modify or add attributes to an item. It can add elements at a specific index in a list or nested attributes like we saw earlier. We can use set to operate on number values. We can use set to operate on numeric values, increasing or decreasing the values.

[02:51] We can combine set with other expressions such as ListAppend to append a value to the end of a list instead of setting a specific index like we saw earlier. We can also combine set with if not exist.

[03:03] Since update is much more granular than put, if not exist helps us to apply similar conditions that we might use in a put on a much more granular attribute level. The remove clause allows us to remove any attribute, as well as specific items from a list, while delete does the same presets.

[03:19] All in all when working with update, you'll find yourself using update expressions with set clauses more often than not. Also remember that put an update operates slightly differently when it comes to when they try to insert a value, overwrite or update one.