简体   繁体   English

boto3、python:将值附加到 DynamoDB 字符串集

[英]boto3, python: appending value to DynamoDB String Set

I have an object in DynamoDB:我在 DynamoDB 中有一个 object:

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

By using boto3 in lambda functions, I would like to add 'con3' to the String Set.通过在 lambda 函数中使用 boto3,我想将“con3”添加到字符串集中。 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.但是,无论我尝试将信息放入字符串集中的方式,它总是运行错误。

Since you're using the resource API, you have to use the Python data type set in your statement:由于您使用的是资源 API,因此您必须使用语句中set的 Python 数据类型:

table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "ADD ConnectionList :i",
    ExpressionAttributeValues = {
        ":i": {"Something"},  # needs to be a set type
    },
    ReturnValues="ALL_NEW"
)

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

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