1

I'm new in AWS and i'm discovering dynamoDB. My table has some items pre-registered. Now i want, let's say, for exemple, get results by filtering two out of the 3 "expressions" (or keys) that each item got : "Id" and "Name"(2 of 3, the 3rd is date). So, i want to fetch them ordering by "date". I set myself "date" for each item where a timestamp has been registered.

I can fetch results by using the "scan" method but it is not really what i want because data are not orderer.

So how can i organize my results by sorting them according to the date key ?

Any help would be highly appreciated

1 Answer 1

1

DynamoDB will return sorted results for you when you have a table or index set up with a Composite Primary Key. This means that two of the attributes are used to create the key. The first attribute is the Partition Key and the second one is the Sort Key which sorts the results WITHIN the partition.

To adapt their example to yours, imagine you have a Songs table and a Global Secondary index with the following key schemas: Songs Table: - ArtistID (partition key) - SongName (sort key) - Timestamp (an attribute) SongsByArtistIndex (Global Secondary Index on the Songs table): - ArtistID (partition key) - Timestamp (sort key) - SongName (a projected attribute)

In this example, you can run 2 types of queries, based on your ability & desire to sort on different attributes.

  1. Alphabetized list of songs by artist: Query the Songs table and set the KeyCondition to the artist that you want.
  2. Songs by artist sorted by date: Query the SongsByArtistIndex and set the KeyCondition to the artist that you want.

You probably need to change your table's key schema to optimize for the sorted queries/access patterns that you need for your scenario. Remember, you can only sort within a partition. Does that make sense?

More info: DynamoDB Query Method, Query & Scan Operations in DynamoDB

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for replying. I created a new table and i defined Id as partition key and time as sort key. Yet, entries are always ordered randomly. Also i can't use de query method when i'm trying to fecth my data via a lambda test function. I can only use scan. When i use query, i get a null return value.
As far as I know, there is no constraint against doing a query in Lambda code. See docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/….
With regards do your table key, how many items do you have with the same Id? Remember, your sorting is happening WITHIN a partition key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.