'DynamoDB Query by Prefix of Partition Key

I have a dynamodb table with following GSI:

  • partition key: scheduled_date which is a date string yyyy-mm-dd HH:MM:SS
  • range key: task_id which is an uuid

I would like to query for all items whose scheduled_date falls in a date, i.e. its prefix matches a string yyyy-mm-dd.

Is it possible without performing scan?



Solution 1:[1]

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LegacyConditionalParameters.KeyConditions.html

You must provide the index partition key name and value as an EQ condition.

In your case, you could consider using yyyy-mm-dd (or yyyymmdd) as the partition key to get all of the items that have that scheduled date.

You could keep task_id as the Range key OR you could use a prefix like HH:MM:SS:task_id. That way the tasks for a particular day would come back sorted by time. And if you really needed to you, could query them by time range.

There is also the alternative of using Global Secondary Indexes that can be utilized in a similar manner.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 adamarla