简体   繁体   English

AWS DynamoDB创建更新表达式-如果不存在则添加新的字符串集

[英]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. 我从此答案中获取了一些代码: 如果不存在,请追加或创建StringSet,但我似乎无法使其工作。

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" } 我最终"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP" }错误"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. 这对我来说可以创建字符串集或附加到现有的字符串集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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