1

So I need to order the query result by the timestamps on items, and I saw that I can use ScanIndexForward, but how can I say that it needs to compare the timestamps?

Thanks!

VardanMelkonyan
  • 486
  • 1
  • 4
  • 11
  • 1
    Does this answer your question? [Is it possible to ORDER results with query or scan in DynamoDB?](https://stackoverflow.com/questions/9297326/is-it-possible-to-order-results-with-query-or-scan-in-dynamodb) – codetiger Aug 12 '20 at 04:22

1 Answers1

1

DynamoDB uses primary keys to uniquely identify items in the database. A primary key is made up of a Partition Key and an optional Sort Key. The Sort Key is what allows DynamoDB to sort your data.

For illustration purposes, imagine you wanted to model an email inbox that allows the user to sort their email by date received. Your table could look like this:

enter image description here

I'm using the users email address as the Partition Key(PK) and a timestamp (date received) as the Sort Key (SK). Together, the PK and SK make up the unique Primary Key that represents a single email to the user.

When you perform a query to get a users emails, DynamoDB will use the Sort Key to sort the results. By default, results will be in ascending order (ScanIndexForward=true) If you want descending order, you'd use ScanIndexForward=false.

Seth Geoghegan
  • 5,372
  • 2
  • 8
  • 23