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?
Amazon DynamoDB TTL allows you to define a per-item timestamp to determine when an item is no longer needed. After the expiration of the TTL timestamp, DynamoDB deletes the item from your table within 48 hours without consuming any write throughput.
"Status" is a reserved keyword per documentation (docs.aws.amazon.com/amazondynamodb/latest/developerguide/…) .
There are multiple ways to represent a timestamp in DynamoDB. Probably the most common is to use a Number type to represent the timestamp with the value as a Unix timestamp (seconds or milliseconds). Additionally, you can store the timestamp as a String type with the value as an ISO 8601 formatted string.
Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With