Reputation: 8140
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:
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.
Upvotes: 0
Views: 522
Reputation: 5179
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.
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
}
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" }
).
Upvotes: 3