简体   繁体   中英

boto3, python: appending value to DynamoDB String Set

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.

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"
)

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