Shushant Arora
Shushant Arora

Reputation: 101

Dynamodb batch put if item does not exist

DynamoDB does not support batch update, it supports batch put only.

But is it possible to batchPut only of item with key does not exist?

Upvotes: 7

Views: 5376

Answers (1)

Daniel Apt
Daniel Apt

Reputation: 2638

In the batchWriteItem, there is the following note:

For example, you cannot specify conditions on individual put and delete requests, and BatchWriteItem does not return deleted items in the response.

Instead, I would recommend using putItem with a conditional expression. Towards the bottom of the putItem documentation there is the following note:

[...] To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the partition key for the table [...]

So make sure to add the following to ConditionExpression (using NodeJS syntax here)

const params = {
    Item: {
        userId: {
            S: "Beyonce",
        }
    },
    ConditionExpression: "attribute_not_exists(userId)"
};

Upvotes: 3

Related Questions