Read the latest first
A sorted timestamp makes 'the most recent' a direction, not a sort in your code.
Why
Every reading from a fridge shares that fridge's partition key, and the sort key is an ISO-8601 timestamp. A fixed-width ISO timestamp sorts lexically in time order, so the newest reading is simply the last one in the partition.
That makes 'the latest' a direction. ScanIndexForward false walks the partition newest-first, and a Limit stops it early. Run all three: direction and limit are request fields, not a second query and a sort back in your own code.
Sort by a fixed-width timestamp and the latest N is a descending Query with a Limit, never a scan-then-sort.
Try it. Then change it.
Predict what each request will do. Run it, inspect the response, then change a value in the workbench and try again.
ScanIndexForward false reads the partition newest-first, and Limit 1 stops at the top row.
Same direction, Limit 3: the three most recent readings, newest first.
Drop ScanIndexForward and the same query reads the other way: the full history, oldest first.
Built on this
- Customer Orders - What did this customer order?
- Sensor Readings - What did this sensor report?
ScanIndexForward false reads the partition newest-first, and Limit 1 stops at the top row.
Choose an access pattern above, or build your own request. See what comes back and what it costs.