简体   繁体   中英

Querying DynamoDB with node.js between values

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\\"",

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
   }
 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM