Is it possible to ORDER results with query or scan in DynamoDB?

Samuel Negru picture Samuel Negru · Feb 15, 2012 · Viewed 96.8k times · Source

Is it possible to ORDER results with Query or Scan API in DynamoDB?

I need to know if DynamoDB has something like [ORDER BY 'field'] from SQL queries?

Thanks.

Answer

Steffen Opel picture Steffen Opel · Feb 15, 2012

Not explicitly, however, ordering is obviously needed for many real world use cases and can be modeled by means of the Hash and Range Type Primary Key accordingly:

In this case, the primary key is made of two attributes. The first attributes is the hash attribute and the second one is the range attribute. Amazon DynamoDB builds an unordered hash index on the hash primary key attribute and a sorted range index on the range primary key attribute. [emphasis mine]

You can then use this range index to optionally request items via the RangeKeyCondition parameter of the Query API and specify forward or backward traversal of the index (i.e. the sort direction) via the ScanIndexForward parameter.

Update: You can order by an attribute with a local secondary index in the same way.