我在Local Dynamo DB中添加了以下对象。
class LicenseEntitilement {
String featureName; // Primary key
int EntitilementCount;
int UsageCount;
}
我需要使用以下表达式进行条件更新 -
条件表达式为:(UsageCount + askCount)< EntitilementCount askCount是一个整数。
(UsageCount和EntitlementCount值在DB中,askCount是局部变量。)
更新表达式为:UsageCount = UsageCount + requestedCount
它抛出一个异常,即条件表达式中不允许使用'+'。有没有办法实现这种条件更新?
答案 0 :(得分:0)
以下是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
}