Nanna
Nanna

Reputation: 575

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.

Upvotes: 0

Views: 1100

Answers (2)

Ankush Jain
Ankush Jain

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. enter image description here

To ensure uniqueness in composite primary key, you can store some random string in sort key column.

Upvotes: -1

Ross Williams
Ross Williams

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

Related Questions