Get the Max Date / Get the last added item in Dyna

2019-07-25 11:45发布

Hi This is my table schema

Contacts table:

"KeySchema": [{
    "AttributeName": "DateTimeRetrieved",
    "KeyType": "HASH"
},{
    "AttributeName": "Id",
    "KeyType": "RANGE"
}]

My primary key has DateTimeRetrieved as the primary key. I would like to retrieve the latest datetime that was entered into the table. It is also equal to the last added items datetime.

Is there a way to query this? . Thanks!

2条回答
Evening l夕情丶
2楼-- · 2019-07-25 12:04

This is not recommended: If your table is small you can always Select all the data and order it in the application level.

It is a bit abusive to try and find order in hash keys. Ordering is done on range and secondary indices. To use DynamoDB efficiently you should model the data as "buckets" (hash keys) and ask questions using Query on the 2nd level.

Without knowing what you're actually modeling it is hard to help but a good read is the Local Secondary Indexes that allows you to have query even on an attribute. With range / LSI you can give order to rows and query the "last" one.

查看更多
Fickle 薄情
3楼-- · 2019-07-25 12:12

I would put the date in the range, in order to perform Query with "startsWith" parameter.

For example:

Key: Date in YYYY-MM Range Key: Date in DD + Id

So, you can issue queries like "give me all the items for 2013-12 for the days starting with "0" (that would give you all the items from the 01 til -09 of December 2013).

Then at the application level, you can parse the ID

查看更多
登录 后发表回答