I'm trying to update an item using updateExpression, and i'd like to edit an attribute and delete another attribute on the same object, but i get the following error:
Invalid UpdateExpression: Syntax error; token: EOF near: "attributeToDelete"
I have to admit i wrote the update expression guessing the proper way to use it (failing). Here's the code snippet:
dynamodb.updateAsync({
TableName: `myTable`,
Key: { id: req.params.id },
UpdateExpression: 'SET attributeToEdit = :newValue DELETE attributeToDelete',
ExpressionAttributeValues: { ':newValue': 'valueToSet' },
})
How am i supposed to write an UpdateExpression when I want to include more than a action? (where actions are -> SET, DELETE, REMOVE, ADD)
With the DynamoDB API, you use the DeleteItem action to delete data from a table, one item at a time. You must specify the item's primary key values. In addition to DeleteItem , Amazon DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.
To delete a single item, use DeleteItem. To delete multiple items, use BatchWriteItem. Despite the naming of BatchWriteItem , it can be used to put multiple items or to delete multiple items, and you can target one or more DynamoDB tables in the same API call.
To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.
Conditional put 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 REMOVE
rather than DELETE. DELETE
is used to delete element from SET.
UpdateExpression: 'SET attributeToEdit = :newValue REMOVE attributeToDelete',
REMOVE - Removes one or more attributes from an item.
I struggled with this for a long while, so I figured a second example may be helpful for future searchers. This example is using CakePHP, Marshal JSON, SET and ADD, and includes variables to show a couple of different ways to get what you want. You'd obviously also pass in all the variables in the overall function calling this.
$key = $this->marshaler->marshalJson('
{
"field_one": ' . $variableOne . '
}
');
if ($criteriaOne === null) {
$add = $this->marshaler->marshalJson('
{"0":{
"field_two": ' . $variableTwo . ',
"field_three": "' . $variableThree . '"
}}
');
} else {
$add = $this->marshaler->marshalJson('
{"0":{
"field_two": ' . $variableTwo . ',
"field_three": "' . $variableThree . '"
"field_four": "null"
}}
');
}
$eav = [
":vals" => [
"L" => [$add[0]]
],
":vals1" => [
"NS" => ["$variableTwo"]
]
];
$ean = [
"#co" => "column_one",
"#ct" => "column_two"
];
return [
'TableName' => 'Dynamo_Table_Name',
'Key' => $key,
'UpdateExpression' =>
'SET #co = list_append(:vals, #co) ADD #ct :vals1',
'ExpressionAttributeNames' => $ean,
'ExpressionAttributeValues' => $eav,
'ReturnValues' => 'ALL_NEW'
];
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