pythonamazon-web-servicesamazon-dynamodb

How to filter by date range in dynamodb?


I have following data in dynamo , all string fields.

id |   data    | my_date 

1  | some text | 2024-03-21
2. | more text | 2024-09-30
....

i wrote a query as such, to scan items and then filter by date range. I tried this and it seems it works. is there any other way to add a filter like if there I can do BETWEEN start AND end dates?

also, when i run this query, i suppose it scans all the items in my table and then filter based on my date range. is there a different(possibly better ) way to filter based on date range?

{
   TableName: mytable, 
   FilterExpression:'my_date > start AND my_date < end',
   ExpressionAttributeValues: {
        ':start': {'S': '2024-06-30'},
        ':end'  : {'S': '2024-01-01'}
   }
}

Solution

  • is there any other way to add a filter like if there I can do BETWEEN start AND end dates?

    DynamoDB supports BETWEEN keyword as mentioned here

    also, when i run this query, i suppose it scans all the items in my table and then filter based on my date range. is there a different(possibly better ) way to filter based on date range?

    IMO if this is not a frequent query pattern it might be a reasonable choice.

    Otherwise:

    Then, you can use the Query operation with a KeyConditionExpression for efficient date range filtering.

    I would recommend to read aws blog for working with timestamp in order to optimize your filtering with date range: https://aws.amazon.com/blogs/database/working-with-date-and-timestamp-data-types-in-amazon-dynamodb/