DynamoDB - Put item if hash (or hash and range combination) doesn't exist

后端 未结 6 1161
误落风尘
误落风尘 2020-12-05 00:32

Here are my use cases: I have a Dynamo table with a hash + range key. When I put new items in the table, I want to do a uniqueness check. Sometimes I want to guarantee th

6条回答
  •  渐次进展
    2020-12-05 00:46

    This version of explanation taken from amazon aws forum says that a search will look an item that matches a provided hash key and then only checks if the attribute exists in that record. It should works the same if you have a hash and a range keys, I suppose.

    If a request try to find an existing item with hash key "b825501b-60d3-4e53-b737-02645d27c2ae". If this is first time this id is being used there will be no existing item and "attribute_not_exists(email)" will evaluate to true, Put request will go through.

    If this id is already used there will be an existing item. Then condition expression will look for an existing email attribute in the existing item, if there is an email attribute the Put request will fail, if there is no email attribute the Put request will go through.

    Either way it's not comparing the value of "email" attribute and it's not checking if other items in the table used the same "email" value.

    If email was the hash key, then request will try to find an existing item with hash key "tielur@example.me".

    If there is another item with same email value an existing item will be found. Since email is the hash key it has to be present in the existing item and "attribute_not_exists(email)" will evaluate to false and Put request will fail.

    If "email" value is not used before existing item will not be found and "attribute_not_exists(email)" will evaluate to true hence Put request will go through.

提交回复
热议问题