Trying to learn DynamoDB here. And I wonder if what I'm trying is not possible in dynamodb, since apparently there doesn't exist a "Group by" function.
In our database, we decided to have a composite key. Versioning elements with a sort key, which is an ISOstring with the date of the insert.
[pk Id, sk UpdateDate]
Is it possible to pull all the newest elements from the DB? Meaning Only one of each primary key, but with the latest version? Kind of like a sort? Or should I pull the whole DB, and sort clientsside?
You can easily fetch the most recent version for each individual Id by doing a Query
on the ID and setting ScanIndexForward=False
as well as Limit=1
. That will get you the most recent version for a specific Id.
Getting the most recent version for all Ids can't be done efficiently in DynamoDB as this would span multiple item collections. (All items with the same Partition Key are considered to be in an item collection.)
If you have a separate index where each Id is only present once --> PK = "ALL_IDS", SK="<Id>"
you could query that index to get a list of the ids and then make a query for each Id. That's going to be a lot of network requests.
Depending on the size of the table it might be most efficient to Scan all data and filter on the client side. But this is really not a pattern DynamoDB is built for.