Categories
DynamoDB tutorial

Update an item in the DynamoDB table

In the last tutorial, we learned how to scan/query the inserted item in the DynamoDB table. In this tutorial, we will be learning how to update an item in the DynamoDB table.

We can update a DynamoDB table using the following 3 ways:

  • AWS UI console
  • AWS CLI
  • AWS SDK

AWS console (UI) to Update an item in the DynamoDB table

To update a table form the AWS console, simply login to the AWS console. Choose services > DynamoDB. In the DynamoDB choose the table you want to update the item from. Go to the items tab and search the item you want to edit. Click on the item and you will see a JSON editor to edit the file.

Update an item in DynamoDB table
Update an item in the DynamoDB table

Update using AWS CLI

We can also update an item in the table using the AWS CLI. The prerequisites are that the AWS CLI is installed and the AWS config is in place. With these things in place, you can update using the AWS CLI.

A simple command to update the DynamoDB table looks like the following:

aws dynamodb update-item \
     --table-name books \
     --key '{
       "Author": {"S": "j k rowling"}
     }' \
     --update-expression 'SET #category = :category' \
     --expression-attribute-names '{
       "#Category": "Category"
     }' \
     --expression-attribute-values '{
       ":category": {"S": "Novel"}
     }'

In the above command, we are updating the books table where the author is “j k rowling” to be a value Novel. Let us look at how the arguments work:

–table-name – The name of the table we want to update
–key – The item that has the specified value in this key will be updated. In the above case Author with the name “j k rowling”
–update-expression – The update expression specifies what values we want to update. In this case, we want to update the category field. We are representing this field value as #Category in the –expression-attribute-names and the value for this in the :category whose value is specified in –expression-attribute-values
–expression-attribute-names – Here we specify the names of the attributes we want to update. In our case it is Category and we are naming it as #Category
–expression-attribute-values – In this field, we specify values of the field we want to update. Here we are representing the value {“S”: “Novel”} as :category

Update using AWS go SDK

package main

import (
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
    "fmt"
)

type UpdateValue struct {
    Category string
}

type ItemKey struct {
    Author string
}

func main() {
    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("us-west-2")},
    }

    // Create DynamoDB client
    svc := dynamodb.New(sess)

    updateValue :=     UpdateValue{
        Category: "Novel",
    }

    itemKey := ItemKey{
        Author: "j k rowling",
    }

    expr, err := dynamodbattribute.MarshalMap(itemKey)
    if err != nil {
        fmt.Println(err)
        return
    }

    key, err := dynamodbattribute.MarshalMap(itemKey)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Update item
    input := &dynamodb.UpdateItemInput{
        ExpressionAttributeValues: expr,
        TableName:                 aws.String("books"),
        Key:                       key,
        UpdateExpression:          aws.String("set updateValue.Category = :Category"),
    }

    _, err = svc.UpdateItem(input)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Success")
}

Leave a Reply

Your email address will not be published. Required fields are marked *