5

I'm not understanding by reading Amazon DynamoDB guide if there's a way to order Items after a query/scan.

For example:

--My database table--

        TableName:"Projects",
           Item:{
              "projectOwner": "User1",
              "projectName": "Project1",
              "creationDate": "Wed Nov 20 2016",
              "updateDate": "Wed Nov 29 2016",
         }Item:{
              "projectOwner": "User1",
              "projectName": "Project2",
              "creationDate": "Wed Nov 20 2016",
              "updateDate": "Wed Nov 28 2016",
         }Item:{
              "projectOwner": "User1",
              "projectName": "Project3",
              "creationDate": "Wed Nov 20 2016",
              "updateDate": "Wed Nov 30 2016",
         }

So I want to scan my database and return all the Items ordered by "updateDate". Is there a way to achieve this using dynamoDB params?

        params = {
             TableName: "Projects",
             ProjectionExpression: "projectName",
             KeyConditionExpression: "projectOwner = :userId",
             ExpressionAttributeValues: {
                 ":userId":userId
             }
        };
Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
depelover
  • 53
  • 1
  • 5

1 Answers1

6

There is no Order By in DynamoDB. You have to sort the result set at client side if the attribute is not defined as Sort key.

However, if the updateDate is defined as Sort key, the API has the feature to sort the data by ascending or descending order.

If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.

Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.

notionquest
  • 37,595
  • 6
  • 111
  • 105