DynamoDB use logical evaluation In key condition expression

Viewed 469

Is it possible in DynamoDB to use a logical evaluation on the sort key in a key condition expression?

For example, on a table with partition key ‘department’ and sort key ‘user’, is it possible to build a key condition expression on a query that looks like:

department eq ‘finance’ and (user eq ‘123’ OR user eq ‘321’)
1 Answers

Yes, it's definitely possible to check the keys in condition expressions.

But your example does not have the right syntax. Some comments about the syntax:

  1. In DynamoDB, constants (like 'finance' or 123) are never part of the expression, but given in a separate parameter, ExpressionAttributeValues, and then each constant is referred in the expression by its name, eg., :val. The rationale is that DynamoDB already has a way to encode constants of different types in JSON, there was no reason to invent yet another way to encode these constants inside the expression.
  2. Some attribute names are also reserved in expressions, so you can also use ExpressionAttributeNames and refer to these names as #name. You don't have to do this if the names you chose aren't one of the reserved names.
  3. The equality operator is =, not eq.
Related