Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't update item in DynamoDB

I have been trying to figure out how to update an item in dynamoDB but have not had any success.

I know how to add and item and remove an item but not update.

Here Is my code:

dynamoDB.updateItem({
    "TableName": "mytable",
    "Key": {
        "thing_ID": {"S": "0000"}
    },
    "UpdateExpression": "SET",
    "ExpressionAttributeNames": {
        "SessionID": ""
    },
    "ExpressionAttributeValues": {
        "SessionID": {
            "S": "maybe this works",
        }
    }
})
like image 398
user1395152 Avatar asked Jun 02 '15 02:06

user1395152


People also ask

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

Does DynamoDB Update create if not exists?

UpdateItem behaves as an “UPSERT” operation. This means that if you try to update an item that doesn't exist, DynamoDB will automatically create it for you. Like with PutItem , you can add conditions to your UpdateItem API calls to modify this behavior, but there is no way to implement it service-side.

How long does it take to update DynamoDB?

How long does it take to update DynamoDB item? It's instant once you commit your save. The API operation completes usually in less than 100ms.

What are the limitations of DynamoDB?

DynamoDB transactional API operations have the following constraints: A transaction cannot contain more than 100 unique items. A transaction cannot contain more than 4 MB of data. No two actions in a transaction can work against the same item in the same table.


1 Answers

It looks like you are trying to update an item by using an Expression, and in this case, your UpdateExpression is incorrect. Both the ExpressionAttributeNames and ExpressionAttributeValues are used for placeholder substitution in your expression.

I think your code would look something like this, if you want to set an attribute for an item:

dynamoDB.updateItem({  
    "TableName" : "exampleTable",
    "Key" : {
        "hashAttributeName" : {
            "S" : "thing_ID"
        }
    },
    "UpdateExpression" : "SET #attrName =:attrValue",
    "ExpressionAttributeNames" : {
        "#attrName" : "SessionID"
    },
    "ExpressionAttributeValues" : {
        ":attrValue" : {
            "S" : "maybe this works"
        }
    }
});

This will update an item that looks like this:

{  
    "Item":{  
        "hashAttributeName":"thing_ID"
    }
}

To this:

{  
    "Item":{  
        "hashAttributeName" : "thing_ID",
        "SessionID" : "maybe this works"
    }
}
like image 179
mkobit Avatar answered Oct 12 '22 07:10

mkobit