Geoff McLennan
Geoff McLennan

Reputation: 448

DynamoDB: Query for range key in a set of non-sequential range keys

I have a table that uses a hash key and sort (range) key. My range key is a uuid. I have a case where I have an arbitrary set of range keys that I want to query the database for using DynamoDBMapper. They are in no way sequential, and unfortunately for my purpose there is no way that they could be sequential, so I cannot query a range of keys (i.e. key is between a and b). Is there any way to have a query condition that searches for the range key that is in a set of potential values? Essentially an sql WHERE condition like: rangeKey IN (val1, val2, ...);

According to the docs for DynamoDBQueryExpression this doesn't appear to be possible using setKeyConditionExpression or setRangeKeyConditions. Is there any efficient way of doing this? Or is my best bet iterating through my set of potential values and using the load method to individually retrieve them from the database?

Upvotes: 2

Views: 4193

Answers (1)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5659

IN operation is not supported in Query as per the documentation here:

The sort key condition must use one of the following comparison operators:

  • a = b — true if the attribute a is equal to the value b
  • a < b — true if a is less than b
  • a <= b — true if a is less than or equal to b
  • a > b — true if a is greater than b
  • a >= b — true if a is greater than or equal to b
  • a BETWEEN b AND c — true if a is greater than or equal to b, and less than or equal to c.

The following function is also supported:

begins_with (a, substr)— true if the value of attribute a begins with a particular substring.

Also, you always need the exact hash key to do a Query. If that is OK for your use case, you should Query DynamoDB and apply the IN filter at the application layer.

If you don't have the exact hash key, then do a Scan, which does have an IN operation for filtering. Documentation here.

Upvotes: 3

Related Questions