javascript - How to save Item in dynamodb with GSI condition? - Stack Overflow

I have a dynamodb table that has a Global secondary Index with a range key (email, hashedPassword ).i

I have a dynamodb table that has a Global secondary Index with a range key (email, hashedPassword ). i want to save an item if the email is not duplicated, i used attribute_not_exists but it doesn't work, i also used :

ConditionExpression: "#email <> :email",
ExpressionAttributeNames: {"#email": "email"},
ExpressionAttributeValues: {":email": userInfo.email}

without success.

Can anyone help me please,

Thank you.

I have a dynamodb table that has a Global secondary Index with a range key (email, hashedPassword ). i want to save an item if the email is not duplicated, i used attribute_not_exists but it doesn't work, i also used :

ConditionExpression: "#email <> :email",
ExpressionAttributeNames: {"#email": "email"},
ExpressionAttributeValues: {":email": userInfo.email}

without success.

Can anyone help me please,

Thank you.

Share asked Dec 11, 2017 at 20:30 SmartoopSmartoop 7257 silver badges13 bronze badges 2
  • What is your primary key (hash key and sort key if you have one)? – F_SO_K Commented Dec 12, 2017 at 8:44
  • @Stu hashKey is "Id" – Smartoop Commented Dec 12, 2017 at 17:26
Add a ment  | 

2 Answers 2

Reset to default 6

The condition expression for DynamoDB only works on the item it is working with, and not across items.

In other words, condition expression does not get evaluated against other items.

For example, if you are creating a new item, you can only enforce the email constraint if you use the Primary Key (Partition + Sort Key if you have one) as the unique constraint.

Some options you have:

  • Perform a read before the insert. This is not going to guarantee uniqueness of the email, but should catch a lot of duplicates.
  • Use Email as the Primary Key.
  • Perform a consistent read after the insert, which rolls back the creation

HTH

In case someone has the same problem, here is my approach:

  1. Perform a read on gsi before the insert
  2. Perform TransactWriteItems with ClientRequestToken (using the data of gsi) to prevent multiple requests in a short time

See this Reference.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855104a4597357.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信