Reputation: 575
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.
Upvotes: 0
Views: 1100
Reputation: 7049
Partition and sort keys in a LSI or GSI do not need to be unique. So you can create an LSI, and use date column there as sort key.
This post exactly talks about this - Local Secondary Indexes.
Here is an screenshot from that page.
To ensure uniqueness in composite primary key, you can store some random string in
sort key
column.
Upvotes: -1
Reputation: 632
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#
.
Upvotes: 2