0
votes

I have table in dynamoDB with partition key and range key.

Table structure

Subscriber ID (partition key) | Item Id (Range Key) | Date        |...
123                           | P_345               | some date 1 | ...
123                           | I_456               | some date 2 |
123                           | A_678               | some date 3 | ... 

Now I want to retrieve the data from the table using QueryAsync C# library with multiple scan conditions.

  • HashKey = 123
  • condition 1; Date is between 'some date 1' and 'some date 2'
  • condition 2. Range Key begins_with I_ and P_

Is there any way which I can achieve this using c# dynamoDB APIs? Please help

1

1 Answers

3
votes

You'll need to do the following (I'm not a C# expert, but you can use the following instructions to find the right C# syntax to do it):

  1. Because you are looking for a specific hashkey, this will be a Query request, not a Scan.
  2. You have a begins_with() condition on the range key. You specify that using the KeyConditionExpression parameter to the Query. The KeyConditionExpression will ask for HashKey=123 AND begins_with(RangeKey,"P_").
  3. However, KeyConditionExpression does not allow an "OR" (rangekey begins with either "P_" or "I_"). You'll just need to run two separate queries - one with "I_" and one with "P_" (you can even do the two queries in parallel, if you wish).
  4. The date is not one of the key columns, so you will need to filter it with a FilterExpression parameter to the query. Note that filtering only happens in the last step, after DynamoDB already read all the items matching the KeyConditionExpression above (this may increase your costs if filtering removes a lot of items and you will still pay for them).