Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a basic sort in DynamoDb?

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.

like image 905
Jamie Lara Avatar asked May 30 '17 23:05

Jamie Lara


People also ask

How does DynamoDB sort?

DynamoDB uses the partition key as an input to the hash function. The hash function's output decides which partition the item will be placed in, and all items with the same partition key value are stored together. The sort key is used to sort and order items in a partition.

How do I add a sort key to my DynamoDB table?

DynamoDB doesn't allow to add a sort key for an already existing table. Your only option is to create a new table with redefined key attributes and then copy the data from the existing table to the newly created table.

Does DynamoDB need a sort key?

Each item in a DynamoDB table requires that you create a primary key for the table, as described in the DynamoDB documentation. A primary key can be a partition key or a combination of a partition key and sort key. The primary key must be unique across the table.

What is the basic data model in Amazon DynamoDB?

A DynamoDB table design corresponds to the relational order entry schema that is shown in Relational modeling. It follows the Adjacency list design pattern, which is a common way to represent relational data structures in DynamoDB.


2 Answers

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.

like image 164
notionquest Avatar answered Sep 29 '22 11:09

notionquest


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

like image 37
Srini Sydney Avatar answered Sep 29 '22 11:09

Srini Sydney