By default DynamoDB will make a new entry if an object with the specified index doesn't exist. Is there a way to stop this from occurring? I could just query the table for the key before updating, but it would be nice to do it all in one request.
UpdateItem behaves as an “UPSERT” operation. This means that if you try to update an item that doesn't exist, DynamoDB will automatically create it for you. Like with PutItem , you can add conditions to your UpdateItem API calls to modify this behavior, but there is no way to implement it service-side.
Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.
Conditional writes. By default, the DynamoDB write operations ( PutItem , UpdateItem , DeleteItem ) are unconditional: Each operation overwrites an existing item that has the specified primary key.
The PutItem operation overwrites an item with the same key (if it exists). If you want to avoid this, use a condition expression. This allows the write to proceed only if the item in question does not already have the same key.
Use conditional expression id = :id
where id
is the attribute name (or primary key name in your case) and :id
is the value (key of the item you want to update).
Conditional expression is always evaluated before any writing. If that expression doesn't evaluate to true
(and it doesn't if that key doesn't exist or is different), it doesn't update or put a new item.
The key point is that ConditionExpression
is reviewed on different data sets depending on the operation you're performing PutItem or UpdateItem.
PutItem.
When setting ConditionExpression
DynamoDB will check your condition on any of the Key rows - many rows if using range attribute on the table or just 1 if only using a hash for your table -.
Remember DynamoDB PutItem operation has to check if the Key you pass already exists, so no extra cost for checking your condition here.
For example, if you have a CUSTOMER_CONTACTS table with customer_id/contact_email key definition and don't want to create duplicates, you can set ConditionExpression = "#contact_email <> :email"
. In that case PutItem operation will fail with ConditionalCheckFailedException if the same email (range attribute) is used for the specified hash value.
But don't expect to check for item attributes far from your hash rows. No sense for DynamoDB to scan all the table just for checking your condition.
Update Item.
If you try the same condition as for previous example ConditionExpression = "#contact_email <> :email"
the operation is always overriding without triggering an exception. Why? Because UpdateItem is just looking at 1 item, the one specified by your Key.
When using UpdateItem the ConditionExpression will look at only 1 row, the one specified by the Key values you have to set. No way to check your condition on any other table row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With