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:

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay