Node.js Dynamodb,根据其中一个参数以相反顺序获取项目的查询?

Node.js Dynamodb,根据其中一个参数以相反顺序获取项目的查询?,node.js,amazon-dynamodb,Node.js,Amazon Dynamodb,我正在寻找聊天记录。现在,每个聊天信息都被保存为dynamodb中的一个项目,我想获取最后的信息,比如说10 msg。我正面临着这个问题。 请参阅, 我已将我的表格定义为: chatTable: Type: AWS::DynamoDB::Table DeletionPolicy: Retain Properties: TableName: Chats KeySchema: - AttributeName: g

我正在寻找聊天记录。现在,每个聊天信息都被保存为dynamodb中的一个项目,我想获取最后的信息,比如说10 msg。我正面临着这个问题。 请参阅,

我已将我的表格定义为:

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:查询条件缺少关键架构元素

你能帮我做些什么让这个查询正常运行吗。
谢谢。

KeyConditionExpression
中,您指定了一个名为
groupId
的属性,但分区键名为
id
。除非您指定
id

我将partion键更改为groupId,否则查询将无法工作,但它不会获取为其创建GSI的最新项。主要问题是使“ScanIndexForward:true”起作用,并从查询中获取最新的x项。您是否也更改了GSI分区键?请更正您的问题,因为模式不是您正在使用的模式,这就是您的问题所在。我已根据您的答案更新了问题中的模式和查询。请检查GSI partition key Range是发送消息的时间戳,我无法根据它进行查询。