typescript 如何使用GSI索引和FilterExpression查询AWS DynamoDb

8ulbf1ek  于 2022-12-14  发布在  TypeScript
关注(0)|答案(1)|浏览(129)

我的DynamoDB数据库表如下所示,它有主键(ID)和排序键(receivedTime)。

ID(primary key)   receivedTime(sort key)     Data
  ID1               1670739188                  3
  ID2               1670723198                  5
  ID2               1674785188                  7
  ID3               1670721388                  5

我想按ID和接收时间查询范围:-
例如:
标识-标识2、
范围- 1670723188到1673723188
我创建了GSI索引,主键是ID。我编写的查询代码如下所示,

ID const params = {
    IndexName: 'Query-ID-index',
    KeyConditionExpression: '#ID = :ID',
    FilterExpression:
      '#receivedTime BETWEEN :startTime AND :endTime ',
    ExpressionAttributeNames: {
      '#ID': 'ID',
      '#receivedTime': 'receivedTime',
    },
    ExpressionAttributeValues: {
      ':ID': id,
      ':startTime': startTime,
      ':endTime': endTime,
    },
    TableName: 'tableName',
  };
  const data = await docClient.query(params).promise();

但是数据.项是空的.任何一项都不能帮助怎么解决。

nxowjjhe

nxowjjhe1#

为什么需要索引?从主表执行Query

const params = {
    KeyConditionExpression: '#ID = :ID AND #receivedTime BETWEEN :startTime AND :endTime',
    ExpressionAttributeNames: {
      '#ID': 'ID',
      '#receivedTime': 'receivedTime',
    },
    ExpressionAttributeValues: {
      ':ID': id,
      ':startTime': startTime,
      ':endTime': endTime,
    },
    TableName: 'tableName',
  };
  const data = await docClient.query(params).promise();

相关问题