Node.JS - 尝试从 DynamoDB 中删除某些项目 table

Node.JS - Trying to delete certain items from a DynamoDB table

我正在尝试从 DynamoDB table 中删除所有具有属性“代码”的记录。这是我的参数:

var params = {
    TableName: table,
    Key:{
        "email": "email",
    },
    ConditionExpression:"attribute_exists(code)",
};

这是错误:

{
  "message": "The conditional request failed",
  "code": "ConditionalCheckFailedException",
  "time": "2021-11-12T09:01:04.205Z",
  "requestId": "e1d74c46-86a0-4f14-8c62-186102f2aff3",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 36.80819226584606
}

示例记录如下所示:

email - first_name - last_name - hash - code - confirmed
e@icloud.com - Bob - Mann - b/x6BkCks6ndXSr/GRhLgOPk.ettTv3lYpglbZbGnM7FXPX5VRFCe - undefined - undefined

根据 documentation:

ConditionalCheckFailedException Message: The conditional request failed.

You specified a condition that was evaluated to be false. For example, you might have tried to perform a conditional update on an item, but the actual value of the attribute did not match the expected value in the condition.

OK to retry? No

所以问题出在 ConditionExpressioncode 属性在某些记录中不存在,或者存在 undefined 值。尝试将 attribute_exists 更改为 attribute_type:

var params = {
    TableName: table,
    Key:{
        "email": "email",
    },
    ConditionExpression:"attribute_type(code, S)",
};

第二个参数:

  • S — 字符串
  • SS — 字符串集
  • N — 数字
  • NS — 号码集
  • B — 二进制
  • BS — 二进制集
  • BOOL — 布尔值
  • 空 — 空
  • L — 列表
  • M — 地图

amazondynamodb 上阅读更多关于比较和功能的信息。