首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用DynamoDB删除亚马逊的LocalSeondaryIndex项目?

如何使用DynamoDB删除亚马逊的LocalSeondaryIndex项目?
EN

Stack Overflow用户
提问于 2020-06-19 18:04:59
回答 1查看 348关注 0票数 0

我在cloudformation中定义了我的dynamoDB如下:

代码语言:javascript
运行
复制
TodosDynamoDBTable:
  Type: AWS::DynamoDB::Table
  Properties:
    AttributeDefinitions:
      - AttributeName: userId
        AttributeType: S           
      - AttributeName: createdAt
        AttributeType: S                                     
      - AttributeName: todoId
        AttributeType: S                                               
    KeySchema:
      - AttributeName: userId
        KeyType: HASH   
      - AttributeName: createdAt
        KeyType: RANGE                                   
    BillingMode: PAY_PER_REQUEST
    TableName: ${self:provider.environment.TODOS_TABLE}
    LocalSecondaryIndexes:
      - IndexName: ${self:provider.environment.TODOID_INDEX}
        KeySchema:
          - AttributeName: userId
            KeyType: HASH        
          - AttributeName: todoId
            KeyType: RANGE               
        Projection:
          ProjectionType: ALL # What attributes will be copied to an index

现在,我想根据userId和todoId查找项目。所以我用了下面的打字本代码:

代码语言:javascript
运行
复制
  const key = {
    userId: userId,
    todoId: todoId
  }

  await docClient.delete({
      TableName: todosTable,
      Key: key
  }).promise();

但是这给了我一个来自CloudWatch的错误

代码语言:javascript
运行
复制
2020-06-19T18:03:17.349Z    00a89359-428f-4a7d-b63c-500e7d062c1b    ERROR   Invoke Error    
{
    "errorType": "ValidationException",
    "errorMessage": "The provided key element does not match the schema",
    "code": "ValidationException",
    "message": "The provided key element does not match the schema",
    "time": "2020-06-19T18:03:17.347Z",
    "requestId": "7EJTPMFQKEUTQAKNI1UVQ0FRF7VV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 6.9291082443874386,
    "stack": [
        "ValidationException: The provided key element does not match the schema",
        "    at constructor.httpResponse (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/protocol/json.js:47:1)",
        "    at constructor.shift [as callListeners] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:100:1)",
        "    at constructor.doneCallback [as emit] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:75:1)",
        "    at constructor.call [as emitEvent] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:683:1)",
        "    at constructor.emit (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:22:1)",
        "    at r.call [as runTo] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/state_machine.js:14:1)",
        "    at runTo (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/state_machine.js:26:1)",
        "    at constructor.done (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:38:1)",
        "    at constructor.call (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/request.js:685:1)",
        "    at constructor.Error [as callListeners] (/var/task/src/lambda/http/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:108:1)"
    ]
}

基于userId和/或todoId的正确的删除语法是什么?

更新: docClient是从标准样板aws、XRay和DynamoDB创建的

代码语言:javascript
运行
复制
const XAWS = AWSXRay.captureAWS(AWS)
const docClient = new XAWS.DynamoDB.DocumentClient()

我还可以使用

代码语言:javascript
运行
复制
  const result = await docClient.query({
    TableName: todosTable,
    IndexName: todoIdIndex,
    KeyConditionExpression: 'userId = :userId and todoId = :todoId',
    ExpressionAttributeValues: {
      ':userId': userId,
      ':todoId': todoId
    }
  }).promise()
EN

Stack Overflow用户

回答已采纳

发布于 2020-06-19 19:47:47

不确定文档客户端是什么,但这应该是您所需要的。基本上先查询,然后删除。这是因为您的表是用userIdcreatedAt索引的,所以您需要在删除条目之前找出它们。

代码语言:javascript
运行
复制
const result = await docClient.query({
    TableName: todosTable,
    IndexName: todoIdIndex,
    KeyConditionExpression: 'userId = :userId and todoId = :todoId',
    ExpressionAttributeValues: {
      ':userId': userId,
      ':todoId': todoId
    },
    ProjectionExpression: 'userId, createdAt'
  }).promise()

if (result.$response.data && result.$response.data.Items) {
    const key = {
        userId: result.$response.data.Items[0]['userId'],
        createdAt: result.$response.data.Items[0]['createdAt']
    }
    await docClient.delete({
        TableName: todosTable,
        Key: key
    }).promise()
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62476137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档