3

Querying dynamoDB table with node.js. The DynamoDB table has key of Timestamp, represented by an integer. In this case, I left :timestampStart as 1 and timestampEnd as 10 as an example.

    var params = {
        TableName: "Table2",
        KeyConditionExpression:"Timestamp = :ts BETWEEN :timestampStart AND :timestampEnd",
        ExpressionAttributeValues: {
            ":ts":"Timestamp",
            ":timestampStart": 1,
            ":timestampEnd": 10
        }
    };

The :ts is not correct, I can see this. I want to return any rows found with a Timestamp value between timestampStart and timestampEnd.

Error message:

"errorMessage": "Invalid KeyConditionExpression: Syntax error; token: \"BETWEEN\", near: \":ts BETWEEN :timestampStart\"",

1 Answer 1

13

If Timestamp is the partition key and not the sort key. Then, you have two problems:

  1. In a Query operation, you cannot perform a comparison test (<, >, BETWEEN, ...) on the partition key. The condition must perform an equality test (=) on a single partition key value and, optionally, one of several comparison tests on a single sort key value. For example:

    KeyConditionExpression: 'HashKey = :hkey and RangeKey > :rkey'

  2. You have a syntax error in your KeyConditionExpression, obviously. Keep in mind that Timestamp is a reserved word in DynamoDB. So, you'll have to use ExpressionAttributeNames for that:

(Assuming you have an Id partition key and Timestamp sort key)

var params = {
   TableName: "Table2",
   KeyConditionExpression: "Id = :id AND #DocTimestamp BETWEEN :start AND :end",
   ExpressionAttributeNames: {
     '#DocTimestamp': 'Timestamp'
   },
   ExpressionAttributeValues: {
     ":id": "SOME VALUE",
     ":start": 1,
     ":end": 10
   }
 };
Sign up to request clarification or add additional context in comments.

2 Comments

If I have Timestamp as the partition key, would creating a secondary global index with the Timestamp as the sort key overcome the problems here? I could then create an equality statement
Solved with the addition of a secondary index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.