简体   繁体   中英

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 eg 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# .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM