DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

1 2 2 1 1

[20 Days of DynamoDB] Day 3 - UpdateItem add-on benefits

Posted: 10/Jan/2024

The DynamoDB UpdateItem operation is quite flexible. In addition to using many types of operations, you can:

  • Use multiple update expressions in a single statement
  • Get the item attributes as they appear before or after they are successfully updated
  • Understand which item attributes failed the condition check (no additional cost)
  • Retrieve the consumed Write Capacity Units (WCU)

Here is an example (using AWS Go SDK v2):

    resp, err = client.UpdateItem(context.Background(), &dynamodb.UpdateItemInput{
        TableName: aws.String(tableName),
        Key: map[string]types.AttributeValue{
            "email": &types.AttributeValueMemberS{Value: email},
        },
        UpdateExpression: aws.String("SET last_name = :ln REMOVE category"),
        ExpressionAttributeValues: map[string]types.AttributeValue{
            ":ln": &types.AttributeValueMemberS{
                Value: lastName,
            },
        },
        ReturnValues:                        types.ReturnValueAllOld,
        ReturnValuesOnConditionCheckFailure: types.ReturnValuesOnConditionCheckFailureAllOld,
        ReturnConsumedCapacity:              types.ReturnConsumedCapacityTotal,
    }
Enter fullscreen mode Exit fullscreen mode

Recommended reading:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay