Adarsh
Adarsh

Reputation: 3583

DynamoDB equivalent to find().sort()

In mongoDB one can get all the documents sorted asc/desc on a particular field as follows:

db.collection_name.find().sort({field: sort_order}) 

query() requires one to specify the partition key and if one wants to query on a non key attribute, A GSI can be created and queries can be run on it for the same as explained here: Query on non-key attribute

scan() would do the job but doesn't provide an option to sort on any field.

One of the solution as described here: Dynamodb scan in sorted order is to have a common key for all documents and create a GSI on the attribute.

But as listed in the comments of the one of the solutions and I quote "The performance characteristics of a DynamoDB table apply the same to GSIs. A GSI with a single hash key of "OK" will only ever use one partition. This loses all scaling characteristics of DynamoDB".

Is there a way to achieve the above that scales well?

Upvotes: 1

Views: 284

Answers (1)

F_SO_K
F_SO_K

Reputation: 14859

The only sorting applied by DynamoDB is by range key within a partition. There is no feature for sorting results by arbitrary field, you are expected to sort your own results in your application code. i.e. do a scan and sort the results on the client side.

Upvotes: 2

Related Questions