I have been trying to figure out how to update an item in dynamoDB but have not had any success.
I know how to add and item and remove an item but not update.
Here Is my code:
dynamoDB.updateItem({
"TableName": "mytable",
"Key": {
"thing_ID": {"S": "0000"}
},
"UpdateExpression": "SET",
"ExpressionAttributeNames": {
"SessionID": ""
},
"ExpressionAttributeValues": {
"SessionID": {
"S": "maybe this works",
}
}
})
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.
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.
How long does it take to update DynamoDB item? It's instant once you commit your save. The API operation completes usually in less than 100ms.
DynamoDB transactional API operations have the following constraints: A transaction cannot contain more than 100 unique items. A transaction cannot contain more than 4 MB of data. No two actions in a transaction can work against the same item in the same table.
It looks like you are trying to update an item by using an Expression, and in this case, your UpdateExpression
is incorrect. Both the ExpressionAttributeNames
and ExpressionAttributeValues
are used for placeholder substitution in your expression.
I think your code would look something like this, if you want to set an attribute for an item:
dynamoDB.updateItem({
"TableName" : "exampleTable",
"Key" : {
"hashAttributeName" : {
"S" : "thing_ID"
}
},
"UpdateExpression" : "SET #attrName =:attrValue",
"ExpressionAttributeNames" : {
"#attrName" : "SessionID"
},
"ExpressionAttributeValues" : {
":attrValue" : {
"S" : "maybe this works"
}
}
});
This will update an item that looks like this:
{
"Item":{
"hashAttributeName":"thing_ID"
}
}
To this:
{
"Item":{
"hashAttributeName" : "thing_ID",
"SessionID" : "maybe this works"
}
}
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