Skip to content
Advertisement

Update values in Dynamodb by modifying the previous values

I created a dynamodb table and I am using the lambda function to update the values in the dynamodb table. The problem with my function for updating the values, not modifying the older values which were already present in the dynamodb, it just overwriting the values. My code snippet for updating the data item in Dynamodb table:

def push_record_daily_table(date,time,total_records):
    table = dynamodb.Table('daily-metrics')
    try:
        table.update_item(
                Key={'date':date},
                UpdateExpression = "SET last timestamp = :label",
                ExpressionAttributeValues={
                    ':label':time
                }
            )
        table.update_item(
                Key={'date':date},
                UpdateExpression = "SET total files +=  :incr",
                ExpressionAttributeValues={
                    ":incr":1
                }
            )
        table.update_item(
                Key={'date':date},
                UpdateExpression = "SET total records += :label",
                ExpressionAttributeValues={
                    ":label": total_records
                }
            )

Here Key is date. I am processing multiple files which have multiple records so everytime this function is being called total files will increase by 1

Advertisement

Answer

DynamoDB update expressions don’t support the += operator. You should use update expressions of the form SET count = count + :incr rather than SET count += :incr.

You didn’t indicate any error message (you should always include error messages) but your attempt to increment the total files columns would have failed with:

botocore.exceptions.ClientError:
An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression:
Syntax error; token: "files", near: "total files +"

If you corrected that, by using an expression name mapping of #totalfiles for total files, your code would have failed with the following, because += is not supported:

botocore.exceptions.ClientError:
An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression:
Syntax error; token: "+", near: "#totalfiles +="

Below is a working example, with a simple attribute name (files) and again with an attribute name containing whitespace (total files).

def push_record_daily_table_corrected(date):
    table.update_item(
        Key={'date':date},
        UpdateExpression = "SET files = files + :incr",
        ExpressionAttributeValues={
            ":incr": 1
        }
    )
    table.update_item(
        Key={'date':date},
        UpdateExpression = "SET #totalfiles = #totalfiles + :incr",
        ExpressionAttributeValues={
            ":incr": 1
        },
        ExpressionAttributeNames={
            "#totalfiles": "total files"
        }
    )
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement