1

I have an object in DynamoDB:

{ 'UserID' : 'Hank', ConnectionList : {'con1', 'con2'} }

By using boto3 in lambda functions, I would like to add 'con3' to the String Set. So far, I have been trying with the following code without success:

ddbClient = boto3.resource('dynamodb')
table = ddbClient.Table("UserInfo")
table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "SET ConnectionList = list_append(ConnectionList, :i)",
    ExpressionAttributeValues = {
        ":i": { "S": "Something" }
    },
    ReturnValues="ALL_NEW"
)

However, no matter the way I try to put the information inside the String Set, it always runs error.

3
  • 1
    What is the error? Does this help? Commented Mar 11, 2021 at 12:40
  • Incorrect operand type for operator or function; operator or function: list_append, operand type: SS", Thank you, but does not work. Tried several other sources before adding a new post on stackoverflow, so far, none of the solution provided work. Commented Mar 11, 2021 at 12:58
  • Does this answer your question? DynamoDB: list_append alternative for sets Commented Mar 11, 2021 at 13:21

1 Answer 1

4

Since you're using the resource API, you have to use the Python data type set in your statement:

table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "ADD ConnectionList :i",
    ExpressionAttributeValues = {
        ":i": {"Something"},  # needs to be a set type
    },
    ReturnValues="ALL_NEW"
)
Sign up to request clarification or add additional context in comments.

4 Comments

It now returns: Incorrect operand type for operator or function; operator or function: list_append, operand type: SS",
I suggest you take a closer look at my answer on the question I linked to earlier and try to see again if it does what you want.
I've updated the code and now it's basically identical, which makes this a duplicate.
table.update_item( Key={ "UserId" : 'Hank' }, UpdateExpression="ADD ConnectionList :set_value", ExpressionAttributeValues={ ":set_value": {'newitem'}, # needs to be a set type } ) with this works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.