Skip to content

Querying a DynamoDb table using a combination of `begins_with` and `between` conditions

How can I query a DynamoDb table using a combination of the BETWEEN and BEGINS_WITH?

A simplified example of my tables partition and sort key looks like this:

PK    |SK
my_pk |2022-05-13T08:30:00Z#tag0
my_pk |2022-05-13T08:45:00Z#tag0
my_pk |2022-05-13T08:45:00Z#tag1
my_pk |2022-05-13T08:45:00Z#tag2
my_pk |2022-05-13T09:00:00Z#tag0
my_pk |2022-05-13T09:00:00Z#tag1

If my SK didn’t include the ...#tag postfix I could use a KeyConditionExpression like this:

"KeyConditionExpression": "#pk = :pk AND #sk BETWEEN :start AND :end"

where start and end are e.g. 2022-05-13T08:45:00Z and 2022-05-13T09:00:00Z, respectively, but I am unsure how to do it when the postfix is there.

Note: I never need to query with the tag, it’s just there to ensure that the composite primary key is unique.

Note2: I am using python and boto3 to execute the query.



You can use between where :end = 2022-05-13T09:00:00Z$. Dollar sign is the next value after hash in ASCII, so this will capture all values starting with 2022-05-13T09:00:00Z#.

User contributions licensed under: CC BY-SA
3 People found this is helpful