Finding the Strongest (Sort Keys)
The Johto region is filled with powerful trainers! To become the best, you need to find the strongest Pokémon. In our Johto Pokédex GSI, we can add a Sort Key to automatically order our queries.
Key takeaways
- Sort Keys automatically organize data within a Partition Key.
- Use ScanIndexForward to control ascending or descending order.
- KeyConditionExpressions are used to match partition and sort keys.
Sort Keys in GSIs
If we set Type1 as the Partition Key and Attack as the Sort Key, DynamoDB automatically keeps our Water-type Pokémon sorted by their Attack power! When querying, you can easily fetch the strongest Water Pokémon by reading the items in reverse order.
Interactive Challenge
You want the strongest Fire types first. The GSI Type1-Attack-Index already sorts by Attack — flip the scan direction so the highest Attack comes back at the top.
ScanIndexForward: false to return results in descending Attack order.Quick Quiz
How do you fetch items in descending order (highest to lowest) using a Query in DynamoDB?
Make your choice to test your understanding. Submit to check, and if it's not quite right you can adjust and try again.