0

I have a dynamo-db table with following schema

{
  "id": String [hash key]
  "type": String [range key]
}

I have a usecase where I need to fetch last 3 rows for a given id when type is unknown.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
Alex
  • 31
  • 5

2 Answers2

0
  1. Add a field to store the timestamp to the schema
  2. Use query to fetch all the records for the given key
  3. Query always returns records sorted by range key, you cannot set a sort order (without changing table's schema), so, sort the records by timestamp in your code
  4. Get top 3 records

If you have a lot of records, use filter expressions to drop extra results. E.g. if you know that latest records will always have a timestamp not older than a hour (day, week or so) you could filter older records.

madhead
  • 31,729
  • 16
  • 153
  • 201
0

Your items need a timestamp attribute. Without that they can’t be sorted out filtered by time. Once you have that, you can define a local secondary index with the id as partition key and the timestamp as the sort key. You can then get the top three items from the index.

Find more information about DynamoDb’s Local Secondary Index here.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108