Search code examples
amazon-web-servicesamazon-dynamodb

How to update a nested field in dynamodb via cli?


I have a record saved in dynamodb:

{
 ...
 st: {
   fs: {
      se: true
   }
  }
}

when I run the command to update st->fs->se in this record,

aws dynamodb update-item --table-name tableName --key '...' --update-expression 'SET #st.#fs.#se= :c' --expression-attribute-values '{":c": {"BOOL": false}}' --expression-attribute-values '{":c": {"BOOL": false}}'  --expression-attribute-names '{"#st": "st", "#fs":"fs", "#se": "se" }'

I got error:

An error occurred (ValidationException) when calling the UpdateItem operation: The document path provided in the update expression is invalid for update

How can I update the nested field?


Solution

  • This works without issue from my side, here is the code:

    TABLE_NAME='TestTable001'
    
    aws dynamodb create-table --table-name $TABLE_NAME \
    --attribute-definitions AttributeName=PK,AttributeType=S \
    --key-schema AttributeName=PK,KeyType=HASH \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
    
    aws dynamodb wait table-exists --table-name $TABLE_NAME
    
    aws dynamodb put-item --table-name $TABLE_NAME \
    --item '{
      "PK": {
        "S": "LHNNG"
      },
      "mymap": {
        "M": {
          "st": {
            "M": {
              "fs": {
                "M": {
                  "se": {
                    "BOOL": true
                  }
                }
              }
            }
          }
        }
      }
    }'
    
    aws dynamodb update-item --table-name $TABLE_NAME \
    --key '{"PK": {"S": "LHNNG"}}' \
    --update-expression "SET #m.#st.#fs.#se = :c" \
    --expression-attribute-names '{"#m":"mymap", "#st":"st", "#fs":"fs", "#se":"se"}' \
    --expression-attribute-values '{":c":{"BOOL": false}}'
    

    I would suggest examining the exact path of your attribute.