dynamodb:如何在地图中增加一个值 [英] dynamodb: how to increment a value in map

查看:216
本文介绍了dynamodb:如何在地图中增加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用dynamodb来维护名称的地图及其值

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

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

我也希望使用增量运算符执行原子增量.但是,我几乎找不到关于如何使用/更新dynamodb映射的文档.这是我到目前为止拥有的python代码

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'}})

推荐答案

DynamoDB更新项目使用ExpressionAttributeNames来防止在表达式中误解属性名称中的特殊字符.

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

您的更新项由键名"player-a"组成,其中带有-"(连字符).

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'
        }
    }
)

这篇关于dynamodb:如何在地图中增加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆