fandang
fandang

Reputation: 607

Query an Amazon DynamoDB by column named "timestamp" (a reserved word)

I have a DynamoDB table with column named "timestamp" and am trying to query by date. I don't have control over changing the column name.

var params = {
    TableName : 'REPORT_CARD',

    KeyConditionExpression: "timestamp BETWEEN :startDate AND :endDate",
    ExpressionAttributeValues: {
        ":startDate": ""+startDate,
        ":endDate": ""+endDate
    }     
}

I get the error:

ERROR: ValidationException: Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: timestamp

Is there a workaround for this besides renaming the "timestamp" column?

Upvotes: 26

Views: 17467

Answers (1)

notionquest
notionquest

Reputation: 39186

Firstly, assuming the table 'REPORT_CARD' is defined with partition key as timestamp, you can use only equality operator (i.e. '=') in KeyConditionExpression for querying data by partition key.

You may use BETWEEN if the timestamp is defined as sort key in the table.

Regarding the keyword error, you can use ExpressionAttributeNames to provide the attribute name timestamp. Example below:-

var params = {
    TableName: 'REPORT_CARD',

    KeyConditionExpression: "parition_key_attr = :partition_val AND #timestamp BETWEEN :startDate AND :endDate",
    ExpressionAttributeNames: { "#timestamp": "timestamp" },
    ExpressionAttributeValues: {
        ':partition_val': "somevalue",
        ":startDate": startDate,
        ":endDate": endDate
    }

Upvotes: 47

Related Questions