简体   繁体   中英

AWS DynamoDB create update expression - Add new stringset if none exists

I am trying to create an update that either adds the email to the string set if the string set exists or creates a string set with the email if it does not exist.

I took some code from this answer: Append to or create StringSet if it doesn't exist but I cant seem to make it work.

I end up with the error "errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP" }

 response = table.update_item(
        Key={'email':email},
        UpdateExpression='ADD emails :i',
        ExpressionAttributeValues={
            ':i': {SS': [email]},
        },
        ReturnValues="UPDATED_NEW"
    )

How can I make an update expression that creates a stringset if none exists or adds an item to it if it does?

I had this same problem this week and I found a solution for anyone who revisits this question later. Take a look at the example below:

response = table.update_item(
    Key={'email': email },
    UpdateExpression="ADD emails :i",
    ExpressionAttributeValues={":i": set([email])},
    ReturnValues="UPDATED_NEW"
)

This worked for me to either create the string set or append to an existing string set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM