简体   繁体   中英

DynamoDB Model/Keys Advice

I was hoping someone could help me understand how to best design my table(s) for DynamoDb. I'm building an application which is used to track the visits a certain user makes to another user's profile.

Currently I have a MongoDB where one entry contains the following fields:

  • userId
  • visitedProfileId
  • date
  • status
  • isMobile

How would this translate to DynamoDB in a way it would not be too slow? I would need to do search queries to select all items that have a certain userId, taking the status and isMobile in affect. What would me keys be? Can I use limit functionality to only request the latest x entries (sorted on date?).

I really like the way DynamoDB can be used but it really seems kind of complicated to make the click between a regular NoSQL database and a key-value nosql database.

There are a couple of ways you could do this - and it probably depends on any other querying you may want to do on this table.

  1. Make your HashKey of the table the userId , and then the RangeKey can be <status>:<isMobile>:<date> (eg active:true:2013-03-25T04:05:06.789Z ). Then you can query using BEGINS_WITH in the RangeKeyCondition (and ScanIndexForward set to false to return in ascending order).

    So let's say you wanted to find the 20 most recent rows of user ID 1234abcd that have a status of active and an isMobile of true (I'm guessing that's what you mean by "taking [them] into affect"), then your query would look like:

     { "TableName": "Users", "Limit": 20, "HashKeyValue": { "S": "1234abcd" }, "RangeKeyCondition": { "ComparisonOperator": "BEGINS_WITH" "AttributeValueList": [{ "S": "active:true:" }], }, "ScanIndexForward": false } 
  2. Another way would be to make the HashKey <userId>:<status>:<isMobile> , and the RangeKey would just be the date . You wouldn't need a RangeKeyCondition in this case (and in the example, the HashKeyValue would be { "S": "1234abcd:active:true" } ).

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