Shaggy31
Shaggy31

Reputation: 35

Updating database only if key is already present

I have to update the dynamodb only if key is present and do not want to create a new row if key is not present.

My Syntax is:

PrimaryKey pk = new PrimaryKey("partitionKey", key1, "sortKey", key2);
String updateExpression = "set abc =:s";
String value = "xyz";

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
                .withPrimaryKey(pk)
                .withUpdateExpression(updateExpression)
                .withValueMap(new ValueMap().withString(":s", value))
                .withConditionExpression(?);
table.updateItem(updateItemSpec);

What should I provide in withConditionExpression. How can I use attribute_exist() here?

Upvotes: 0

Views: 825

Answers (1)

Anshul Saraswat Kaul
Anshul Saraswat Kaul

Reputation: 387

The update item API would create the new item if it doesn't exist. You can use ConditionExpression to stop creating the new item.

ConditionExpression = attribute_exists(partitionKey)

If the key is not found, the API will throw ConditionalCheckFailedException.

Upvotes: 1

Related Questions