這裏是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
}
這種情況對所有情況都沒有幫助。在你的例子中,如果:requestedCount = 3。如果你執行兩次,那麼它也允許第二次。爲了防止這種情況,我們需要以下條件表達式 - (UsageCount + ** askedCount **)