0

put-item: Creates a new item, or replaces an old item with a new item

update-item: Edits an existing item's attributes, or adds a new item to the table if it does not already exist.

When I used update-item with a new partition key which did not exist in the table, it created the item. The same thing happened with put-item.

So what is the different between put-item and update-item?

Thanks.

  • See [Difference between DynamoDb PutItem vs UpdateItem?](https://stackoverflow.com/questions/43667229/difference-between-dynamodb-putitem-vs-updateitem) – dmulter Jul 03 '18 at 15:47

1 Answers1

4

The difference is subtle and it has to do with the scenario when the item already exists in the table.

PutItem will always act as if the item did not exist in the table at all, recreating it entirely with the contents of the new item.

UpdateItem on the other hand, in the case when the item already exists, will not completely recreate/replace the item but instead it will update the attributes of the existing item based on the contents of the new item. The behavior can be configured to merge or remove attributes from the existing item.

I hope this makes sense but think of PutItem as “I don’t care what’s there, make it look like what I’m telling you” vs. UpdateItem which is more like “modify the item, if it exists, to add/remove attributes”

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • An example helps here: if DB has {name: "John", last: "Smith"} An Update with {name: "Joe"} will result in {name: "Joe", last: "Smith"} A Put with {name: "Joe"} will result in {name: "Joe"}. – Acuariano Jul 04 '18 at 13:56