Amazon DynamoDB Query for Items whose key contains a substring

daniel picture daniel · May 9, 2015 · Viewed 20.4k times · Source

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?

Answer

bsd picture bsd · May 9, 2015

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