2

I have a table that has an article a day and it looks like this.

enter image description here

I set date as a primary key and post_id as sort key. I want to make a query that gets the one latest row by date. Is it possible to do it with Query? or I have to use Scan and filter that out?

Skate to Eat
  • 2,554
  • 5
  • 21
  • 53
  • The only way I'm aware of is to scan it and filter it out after getting the results. But I haven't used DynamoDB queries very much. Mainly get and scans. – Charlie Fish Nov 10 '17 at 00:42
  • https://stackoverflow.com/questions/14836600/querying-dynamodb-by-date – Tom Grylls Nov 10 '17 at 08:48

1 Answers1

2

Firstly, DynamoDB doesn't have aggregate functions such as min and max like RDBMS aggregate function. However, it does have one feature to get the latest date from table. In order to use that option, the attribute should be defined as SORT key. Also, the latest date can be found for the specific partition key only i.e. it does not apply for the whole table, just the chosen partition key.

ScanIndexForward can be used to get the latest item for the specific partition key.

ScanIndexForward — (Boolean) Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.

In order to use the above option, the table design has to be changed.

post_id - partition key

date - sort key

Community
  • 1
  • 1
notionquest
  • 37,595
  • 6
  • 111
  • 105