DynamoDb: Query items between two dates

帅比萌擦擦* 提交于 2020-04-16 03:48:08

问题


I'm not having much luck with this query, it returns 0 items

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();

I have one item in the DB:

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

Table:

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}"

I would expect 2018-02-20T02:58:49.710Z to fall between 2017-08-30T03:11:22.627Z and now.

Can you help out with the query?


回答1:


As @cementblocks mentioned, the query works as expected.




回答2:


Have you tried using .scan instead of .query ?

this.dynamoDB.scan(params, onScan); // scan can do filtering 

function onScan(err, data) {
if (err) {
    console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else { 
    //do something with data
}

See documentation for FilterExpression Step 4.3: Scan



来源:https://stackoverflow.com/questions/52089367/dynamodb-query-items-between-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!