2

In my DynamoDB table my primary key is composed of a partition key (documentId - string) and sort key (revision - string).

documentId | revision | details (JSON)
       A   | 5        | { title: "Where's Wally New" }
       A   | 2        | { title: "Where's Wally" }
       B   | 3        | { title: "The Grapes of Wrath" }
       C   | 4        | { title: "The Great Gatsby" }

For a set of documentIds, I want to grab the latest revisions of those documents, as defined by the sort key. For example, I want to get the details of the latest revisions for documentId (A, B). This should return ("Where's Wally New", "The Grapes of Wrath").

I've managed to find people confirming you do this efficiently if you are just looking up one hash key/documentId at a time (e.g. NoSQL: Getting the latest values from tables DynamoDB/Azure Table Storage), but if I want to avoid having to make multiple read queries is this possible?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
friartuck
  • 2,954
  • 4
  • 33
  • 67

2 Answers2

1

You’re looking for a batch query. It doesn’t exist (at least today). See a previous question on this at DynamoDB batch execute QueryRequests

One comment there suggested PartiQL could help. But no. According to https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.querybuilder.partiql.html

“As with the BatchGetItem operation, only singleton read operations are supported. Scan and query operations are not supported.”

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
0

You can sort by Sort Key.
By default results are sorted in ascending order, either numeric order or UTF-8 order. As stated in the docs:

Query results are always sorted by the sort key value. If the data type of the sort key is Number, the results are returned in numeric order; otherwise, the results are returned in order of UTF-8 bytes. By default, the sort order is ascending. To reverse the order, set the ScanIndexForward parameter to false.

To reverse that and have it sorted in descending order, you need to set "ScanIndexForward": false in your query. Now to only receive the top of the list - which will be the most recent revision ie. the highest revision number of that documentId - you can limit the results to one via
"Limit": 1.

However, since you're using strings for your sort key you will have issues with say numbers "9" and "10", since string "10" is of lesser "value" than string "9" since it starts with a "1". I'd recommend switching to numbers for the revision number to resolve that issue.

Cheers!

Avramo
  • 143
  • 9