xDr_Johnx
xDr_Johnx

Reputation: 77

How to perform an ordered query with DynamoDB?

I am using an AWS EC2 instance with DreamFactory API installed. I have connected to a DynamoDB instance.

Is it possible to order a query based on a integer value associated with that row?

Lets say each row in my table has two values: a score (int) and text (string). Is it possible to query data from highest to lowest score using DynamoDB?

Upvotes: 2

Views: 518

Answers (1)

JaredHatfield
JaredHatfield

Reputation: 6651

Typically when you are performing a query you have an item with both a hash and range key and you are only specifying the range key so it is all of those items you are retrieving.

From the DynamoDB documentation:

Query results are always sorted by the range key. If the data type of the range key is Number, the results are returned in numeric order; otherwise, the results are returned in order of ASCII character code values. By default, the sort order is ascending. To reverse the order, set the ScanIndexForward parameter to false.

If you are querying a global secondary index it can be a bit more complicated because a single hash key value is not required to be unique, but the same type of sorting does apply to items with different range keys.

To directly answer your question, yes, it is possible, but you will want these fields to be the range key on either the primary table or as part of a global secondary index.

Upvotes: 3

Related Questions