How to order by in DynamoDB with the AWS SDK?
-
06-12-2019 - |
Question
Currently, my range value is a BigDecimal attained by Time.now.to_f
, and I want to retrieve all the documents of a user, like so:
table = dynamo_db.tables['some_table']
table.load_schema
docs = table.items.where(:user_id => user_id).select.map {|i| i.attributes}
The docs
is ordered in the range value descending.
Solution
After digging into the SDK source code, I was able to find this little useful nugget for the method AWS::DynamoDB::ItemCollection#query
# @option [Boolean] :scan_index_forward (true) Specifies which
# order records will be returned. Defaults to returning them
# in ascending range key order. Pass false to reverse this.
Since my user_id
is the hash value, I was able to revise my query to:
docs = table.items.query(:hash_value => user_id, :scan_index_forward => false).select.map {|i| i.attributes}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow