Thomas Seehofchen
Thomas Seehofchen

Reputation: 557

amazon-dynamodb and filtering by date

I have a dynamodb table which has a GlobalSecondaryIndex with a date field as attribute of keytype "range". Values are stored in ISO-format "YYYY-MM-DD". I want to access in node only records with dates greater than a certain date. It seems to work with a parameter for my query:

KeyConditionExpression: 'myDate >= :myDate',
ExpressionAttributeValues: {   
    ':myDate': '2017-11-17'
}

However I do not quite understand. The date is technically speaking a string. Is it reliable to trust the string comparison or should it be done in a different way?

Upvotes: 10

Views: 23716

Answers (2)

dipesh somale
dipesh somale

Reputation: 11

use this approach for the date filter in the dynamoDb table, and it's working for me.

{
TableName: your-table-name ,
FilterExpression: '#attr2 BETWEEN :start_date AND :end_date',
ExpressionAttributeNames: {
      '#attr2': 'created_at',
},
ExpressionAttributeValues: {
      ':start_date': `2021-04-01T00:00:00`,
      ':end_date': `2021-04-05T23:59:59`
}

I hope this will help for new to aws

Upvotes: 1

notionquest
notionquest

Reputation: 39186

Yes, you can relay on this as long as the date is stored in YYYY-MM-DD format.

DynamoDB doesn't have a separate data type for Date. Date can be stored either as String or Number.

Date    S (string type). The Date values are stored as ISO-8601 formatted strings.

Storing date as String:-

You can use the string data type to represent a date or a time stamp. One way to do this is by using ISO 8601 strings, as shown in these examples:

  • 2016-02-15

  • 2015-12-21T17:42:34Z

  • 20150311T122706Z

Storing date as Number:-

You can use the number data type to represent a date or a time stamp. One way to do this is by using epoch time—the number of seconds since 00:00:00 UTC on 1 January 1970. For example, the epoch time 1437136300 represents 12:31:40 UTC on 17 July 2015.

Data types

Upvotes: 9

Related Questions