Given the following code, I'd like to prevent duplicates where the Sort Key of a given table doesn't already have a Sort key that begins_with 'account_':

            TableName: process.env.TABLE_NAME,
            ConditionExpression: "NOT begins_with(SK,account_)",
            Item: account
        },function(err,data) {
            if(err) {
            } else {

Here's the table format
PK               | SK            | Attributes
user1@domain.com | account_123094| ...
user1@domain.com | account_239123| ... <-- ConditionalExpression should prevent
user2@domain.net | account_993422| ... <-- ConditionalExpression should allow

As seen above, I need to prevent duplicates if there exists a PK already or more specifically if the PK and SK doesn't start with 'account_'. No matter what I try, I keep getting duplicates PK records.


If your use case is to have only one account you sk should only be account instead of account_{someNumber}.

With the structure you are using, if before doing a put you check if some entity with account_* exist and after that do a put call. But that will also not be sufficient to fulfill the given condition.(there can be two guys simultaneously doing a put call)

  • The goal of 'account_{someNumber}' is to construct a query in the future for all expired accounts. Where {someNumber} is an epoch timestamp of when the account should expire. Then I can use a query to find all expired accounts after the current date/timestamp – npn_or_pnp Mar 13 at 13:07
  • 1
    well, for that you can not use this pk, sk combination anyways, since it will require your to do scan(which you should try to avoid by all means). hence it is as good as using another field to store expiry time. – best wishes Mar 13 at 13:16

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.