1
votes

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?

1
The only sorting applied by DynamoDB is by range key. There is no feature for sorting 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.F_SO_K
But scan() wouldn't return all the documents if the data is huge. Its restricted to max of 1MB.Adarsh
If you were using the API directly you would need to use the LastEvaluatedKey to page through the results. However the SDKs do all that for you behind the scenes. So effectively, you do actually get all the results in one scan operation. At least the mechanics of the paging are abstracted away by the SDKs.F_SO_K
Alright. So this is the only way to do it then I suppose. Could you answer the question so that I can close this with an accepted answerAdarsh

1 Answers

2
votes

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.