Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Scan an index in reverse in DynamoDB?

I am currently using DynamoDB and having a problem scanning. I am able to get paged results in forward order by using the ExclusiveStartKey. However, regardless of whether I set ScanIndexForward true or false, I get results in forward order from my scan operation. How can i get results in reverse order from a Scan in DynamoDB?

like image 726
Bharat Bhushan Avatar asked Oct 08 '14 05:10

Bharat Bhushan


Video Answer


1 Answers

As of now the dynamoDB scan cannot return you sorted results.

You need to use a query with a new global secondary index (GSI) with a hashkey and range field. The trick is to use a hashkey which is assigned the same value for all data in your table.

I recommend making a new field for all data and calling it "Status" and set the value to "OK", or something similar.

Then your query to get all the results sorted would look like this:

{
    TableName: "YourTable",
    IndexName: "Status-YourRange-index",
    KeyConditions: {
        Status: {
            ComparisonOperator: "EQ", 
            AttributeValueList: [ 
                "OK"
            ]
        }
    },
    ScanIndexForward: false
}

The docs for how to write GSI queries are found here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html#GSI.Querying

like image 194
Mithu CN Avatar answered Oct 04 '22 15:10

Mithu CN