DynamoDB: 查询两个日期之间的项目

7
我在这个查询上并不太成功,它返回了0个项目。
const { Items } = await this.dynamoDb.query({
  TableName: 'exampleTableName',
  IndexName: 'createdDateTime-index',
  KeyConditionExpression: '#createdDateTime BETWEEN :fromDateTime AND :toDateTime AND #status = :status',
  ExpressionAttributeNames: {
    '#status': 'status',
    '#createdDateTime': 'createdDateTime',
  },
  ExpressionAttributeValues: {
    ':fromDateTime': '2017-02-20T01:58:49.710Z',
    ':toDateTime': new Date().toISOString(),
    ':status': 'SUCCESS',
  },
}).promise();

我在数据库中有一个项目:

{
  "id": "fa47003a-983a-4dc3-a87e-ace73ea7e451",
  "createdDateTime": "2018-02-20T02:58:49.710Z",
  "status": "SUCCESS"
}

表格:

aws dynamodb create-table \
  --endpoint-url http://localhost:8000 \
  --table-name exampleTableName \
  --attribute-definitions \
    AttributeName=id,AttributeType=S \
    AttributeName=status,AttributeType=S \
    AttributeName=createdDateTime,AttributeType=S \
  --key-schema \
    AttributeName=id,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
  --global-secondary-indexes \
    IndexName=createdDateTime-index,KeySchema=["{AttributeName=status,KeyType=HASH},{AttributeName=createdDateTime,KeyType=RANGE}"],Projection="{ProjectionType=ALL}",ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"

我希望 2018-02-20T02:58:49.710Z 落在 2017-08-30T03:11:22.627Z 和现在之间。您能帮忙查询吗?

表格中的完整项目是什么?你展示的那个不正确,缺少了 id 属性。 - cementblocks
抱歉 @cementblocks,我在数据示例中添加了缺失的id字段。 - ChrisRich
你的查询对我来说有效,使用了你的表定义和对象。你在你的Node.js代码中是否访问了本地端点? - cementblocks
我不知道为什么这对我当时没起作用。现在可以了。一定与我的本地设置有关... - ChrisRich
1个回答

0
我测试了下面的代码,它按预期工作:
const { Items } = await this.dynamoDb.query({
  TableName: 'myTable',
  IndexName: 'myIndex',
  KeyConditionExpression: '#createdDateTime BETWEEN :fromDateTime AND :toDateTime AND #status = :status',
  ExpressionAttributeNames: {
    '#status': 'status',
    '#createdDateTime': 'createdDateTime',
  },
  ExpressionAttributeValues: {
    ':fromDateTime': '2017-02-20T01:58:49.710Z',
    ':toDateTime': new Date().toISOString(),
    ':status': 'SUCCESS',
  },
}).promise();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接