Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon DynamoDB Query for Items whose key contains a substring

I am using an Amazon DynamoDB database, and I have a list of items with various strings as the key. I want to query for items whose key contains a substring. For example, if some of the keys are:

"abcd_aaa"
"abcd_bbb"
"abcd_ccc"

I want to query where a key contains "abcd" and these 3 items will be returned. Is this possible?

like image 269
daniel Avatar asked Aug 30 '25 15:08

daniel


1 Answers

You can only query the hashKey using the equality operator (EQ). That being said if those values ("abcd_aaa", "abcd_bbb", "abcd_ccc") belong to your hashKey then you have to provide them entirely. On the other hand, the Query operation does allow partial matching on the rangeKey with the option of a few additional comparison operators:

EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN

See the Query documentation for more details.

One possibility would be to use a hashKey and rangeKey where the first part of your code would be the hashKey and the last the rangeKey, example:

hashKey : abcd
rangeKey : aaa

By doing this when you query by hashKey (abcd), you would receive all three records sorted by the rangeKey

like image 89
b-s-d Avatar answered Sep 15 '25 12:09

b-s-d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!