简体   繁体   中英

Sorting items by dates in DynamoDB

I come from a strict SQL background.

Now migrating to DynamoDB, I have a table full of items which I would like to sort by dates.

Here is how I do it:

  • I set up a secondary index Category-Date-Index. Category is Hash and Date is Range. All items I am sorting will have the same value for category.

  • The problem I now have is that many items have the same dates. This secondary index automatically drops items with the same Category-Date and keeps only one. This is not the behavior I desire.

What would be the right way to do this?

I would also appreciate pointers to a good reading on how to structure tables and indices in DynamoDB when considering these use cases.

how many items with same date do you have?

you can always add to your date an extra postfix (like a random number in range of 0 - X - if your date is an int - epoc time) - this will also ensure your sorting. (only if your range is string, and you always add the same number of digits)

for example:

original item = (hash, 1234567)

converted item = (hash, '1234567010')

original item2 = (hash, 1234567)

converted item2 = (hash, '1234567900')

you can use 'overwrite' param (and set it to false) when inserting an item. in case of an error, you can add an extra number to your range key.

you can find good guidelines here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM