6

I want to update an item in Dynamodb such that I can set the value of one attribute based on the value of an existing attribute in that item. For example: I have table with the following item {"id": 1, "valueone": 30}.

I want to update this item such that I can add another attribute valuetwo whose value is twice that of valueone: {"id": 1, "valueone": 30, "valuetwo": 60}

Something like this, but not sure how to represent valueone in the ExpressionAttributeValues:

table.update_item(Key={'id': 1}, UpdateExpression="set vtwo = :two * :r", ExpressionAttributeValues={':r': valueone, ':two': 2},ReturnValues="ALL_NEW"))

I can control whether valuetwo is a new attribute in the item or already exists with a dummy value (say zero).

1
  • how u acheived it in aws cli? Commented Nov 14, 2021 at 19:35

1 Answer 1

6

Currently, DynamoDB doesn't support " * - Multiply (or) / - Division" arithmetic operators in UpdateExpression.

It supports only addition (+) and subtraction (-) operators.

Addition:-

UpdateExpression : "SET total_new_val = total_val + :value",

Subtraction:-

UpdateExpression : "SET total_new_val = total_val - :value",

Multiplication:-

UpdateExpression : "SET total_new_val = total_val * :value",    

Multiplication throws the below error:-

Unable to update item. Error JSON: {
  "message": "Invalid UpdateExpression: Syntax error; token: \"*\", near: \"tota
l_val * :value\"",
  "code": "ValidationException",
  "time": "2017-02-07T11:32:41.478Z",
  "requestId": "64f36024-7251-40af-98ee-e9cef854e94b",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to get around this constraint by doing something like this: table.update_item(Key={'id': 1}, UpdateExpression="set vtwo = :r", ExpressionAttributeValues={':r': valueone *2},ReturnValues="ALL_NEW"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.