Dynamodb BETWEEN operation for strings

Viewed 4288

I cannot find any detailed documentation for dynamodb BETWEEN operator especially how it deals with strings.

For example if I make queries like "... sort_key Between '100' and '150' " and sort key is "120" then it works correct. But if I make strings like "1224|67" or "6653|67" and then query for between "3456|67" and "8968|67", it doesn't work. How to get information how this comparison works internally?

1 Answers

In your case you need to use expression attribute names and expression attribute values. This is a very good practice in any DynamoDB query.

Please note that you'll also need to set a partition key (see #pk and :pk below).

aws dynamodb query \
    --table-name TABLE_NAME \
    --key-condition-expression "#pk = :pk AND #sk BETWEEN :a and :b" \
    --expression-attribute-names '{"#sk": "sort_key", "#pk": "partition_key"}' \
    --expression-attribute-values '{":a": {"S":"3456|67"}, ":b": {"S":"8968|67"}, ":pk":{"S":"partition_key_value"}}'

I didn't use this syntax for a while, there may me some small syntax error, but this is what you have to do in order to query with BETWEEN.

See the last example about KeyConditions:

aws dynamodb query \
    --table-name Music \
    --key-condition-expression 'Artist = :a AND SongTitle BETWEEN :t1 AND :t2' \
    --expression-attribute-values '{
        ":a": {"S": "No One You Know"}, 
        ":t1": {"S": "A"}, 
        ":t2": {"S": "M"}
    }'
Related