dynamodb: how to increment a value in map

痞子三分冷 提交于 2020-12-14 12:33:26

问题


I am trying to use dynamodb to maintain a map of names with their values

eg. {"scores": {"player-a": 10}}

I also wish to use increment operator to perform atomic increments. However, i could find very little documentation on how to use/update dynamodb maps. Here's the python code I have so far

 import boto3
 ddb = boto3.client('dynamodb')
 ddb.update_item(TableName='ledger', Key={'week': {'S': '06-12'}}, 
                 UpdateExpression='SET scores.player-a  = scores.player-a + :val', 
                 ExpressionAttributeValues={':val': {'N': '12'}})

回答1:


DynamoDB update item uses ExpressionAttributeNames to prevent special characters in an attribute name from being misinterpreted in an expression.

Your update item consists of "player-a" as a key name which has "-" (hyphen) in it.

ddb.update_item(
    TableName='ledger',
    Key={
        'week': {
            'S': '06-12'
        }
    }, 
    UpdateExpression='SET scores.#s = scores.#s + :val",  
    ExpressionAttributeNames={
        "#s": "player-a"
    },  
    ExpressionAttributeValues={
        ':val': {
            'N': '12'
        }
    }
)


来源:https://stackoverflow.com/questions/38163082/dynamodb-how-to-increment-a-value-in-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!