Dynamodb,查询基于参数之一以相反顺序获取项目?

时间:2018-08-06 12:31:24

标签: node.js amazon-dynamodb

我正在寻找聊天记录。现在,每个聊天消息都保存为dynamodb中的一个项目,我想获取最后一个说的10 msgs。我在那方面面临问题。 请参阅

我将表格定义为:

chatTable:
      Type: AWS::DynamoDB::Table
      DeletionPolicy: Retain
      Properties:
        TableName: Chats
        KeySchema:
          - AttributeName: groupId
            KeyType: HASH
          - AttributeName: timestamp
            KeyType: RANGE
        GlobalSecondaryIndexes:
          - IndexName: timestampIndex
            KeySchema:
              - AttributeName: groupId
                KeyType: HASH
              - AttributeName: timestamp
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 10
              WriteCapacityUnits: 5
        AttributeDefinitions:
          - AttributeName: groupId
            AttributeType: S
          - AttributeName: timestamp
            AttributeType: N
        ProvisionedThroughput:
          ReadCapacityUnits: 10
          WriteCapacityUnits: 5

我正在尝试查询以获取以下群组之一的聊天记录:

let getChatParams = {
      TableName: "Chats",
      IndexName: "timestampIndex",
      Limit: 10,
      ScanIndexForward: true,
      KeyConditionExpression: "groupId = :groupId",
      ExpressionAttributeValues: { ":groupId": groupId }
    };

    let groupChat = await docClient.query(getChatParams).promise();

但是我遇到了错误,

  

ValidationException:查询条件缺少键架构元素

请问我需要做些什么才能使此查询正常运行。 谢谢。

1 个答案:

答案 0 :(得分:0)

在您的KeyConditionExpression中,您指定了一个名为groupId的属性,但您的分区键被称为id。除非您指定id,否则查询将无法工作。