8

Dynamodb can make the simplest of database operations difficult. I have the following table and all I want to do is simply sort by the due column. How is this achieved in DynamoDb? I read everything I could find online and there doesn't seem to be a straightforward walk through anywhere.

payor    |  amount  |  due  | paid
----------------------------------
Ally     |  200.00  |   13  |    1
Chase    |   80.00  |    2  |    0
Wells    |   30.00  |   17  |    1
Directv  |  150.00  |    5  |    0

So without considering the payor, amount or paid columns, how can I simply sort on the due column.

aldo.roman.nurena
  • 1,323
  • 12
  • 26
Jamie Lara
  • 165
  • 1
  • 6
  • Are you doing a **query** or a **scan**? See also: [Is it possible to ORDER results with query or scan in DynamoDB?](https://stackoverflow.com/q/9297326/174777) and [Dynamodb scan in sorted order](https://stackoverflow.com/q/21794945/174777) – John Rotenstein May 30 '17 at 23:48

2 Answers2

7

Simply, this can't be achieved in DynamoDB if the due attribute is not defined as sort key. Even if you define the due attribute as sort key, the ordering can be done only within the particular partition key. The ordering can't be done across the partition key.

Assume, you have defined the due as sort key of the table. You can use ScanIndexForward to true/false to order the items in ascending / descending order.

notionquest
  • 37,595
  • 6
  • 111
  • 105
2

Data modeling in dynamo db involves designing the partition key and then determining the sort key for a use case. Partition key is compulsory for any query. This is a basic design premise of a key value nosql store which is completely different than a relational store

Srini Sydney
  • 564
  • 8
  • 17