Amazon Local DynamoDB中的条件更新



我已经将以下对象放在本地发电机DB中。

class LicenseEntitilement {
String featureName;       // Primary key
int EntitilementCount;
int UsageCount;
}

我需要使用以下表达式进行有条件的更新 -

有条件的表达方式为:( usagecount questCount(<权威性询问是一个整数。

(usagecount和AstrementCount值在db中,询问是局部变量。(

更新表达式为:usagecount = usagecount questCount

这是一个例外,即有条件的表达不允许" "。有什么方法可以实现此条件更新?

这是ConditionExpression的定义。

必须满足的条件才能有条件更新 成功。

查看以下条件表达式。最初,表中的项目具有以下值: -

UsageCount = 1

EntitilementCount = 5

本地变量: askedCount = 1(即将值递增1(

var params = {
    TableName: "tableName",
    Key: {
        "ID": '3'
    },
    UpdateExpression: "SET UsageCount = UsageCount + :askedCount",
    ConditionExpression : "UsageCount < EntitilementCount",
    ExpressionAttributeValues: {
        ":askedCount": askedCount,
    },
    ReturnValues: "UPDATED_NEW"
};

我已经成功执行了4次该程序。第五次,我收到了The conditional request failed错误消息。这满足您的要求。

prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":2}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":3}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":4}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":5}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
Unable to update item. Error JSON: {
  "message": "The conditional request failed",
  "code": "ConditionalCheckFailedException",
  "time": "2017-04-26T20:29:21.710Z",
  "requestId": "d73571f3-17f3-4aca-a3cf-7f13019a0292",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}

最新更新