简体   繁体   中英

What's the best way to store users in DynamoDB so I can get one efficiently, and a related group as well?

I have users for my website that need to log in. In order to do that, I have to check the database for them, by email address or a hash of their email.

Some of my users have an online course in common.

Others are all on the same project.

There are multiple projects and courses.

How might I set up my table so that I can grab individual users, and efficiently query related groups of users?

I'm thinking...

PK = user#mysite SK = user@email.com projects = [1,2,3] courses = [101,202,303]

I can get any user user with a get PK = user#mysite, SK = user@email.com .

But if I query, I have to filter two attributes, and I feel like I'm no longer very efficient.

If I set up users like this on the other hand:

PK = user@email.com SK = 1#2#3#101#202#303 projects = [1,2,3] courses = [101,202,303]

Then I can get PK = user@gmail.com and that's unique on its own.

And I can query SK contains 101 for example if I want all the 101 course students.

But I have to maintain this weird # deliminated list of things in the SK string.

Am I thinking about this the right way?

You want to find items which possess a value in an attribute holding a list of values. So do I sometimes. But there is not an index for that.

You can, however, solve this by adding new items to the table.

Your main item would have the email address as both the PK and the SK. It includes attributes listing the courses and projects, and all the other metadata about that user.

For each course, you insert additional items where the course id is the PK and the member emails are the various SKs in that item collection. Same for projects.

Given an email, you can find all about them with a get item. Given a course or project you can find all matching emails with a query against the course or project id. Do a batch get items then if you need all the data about each email.

When someone adds or drops a course or project, you update the main item as well as add/remove the additional indexed items.

Should you want to query by course X and project Y you can pull the matching results to the client and join in the client on email address.

In one of your designs you're proposing a contains against the SK, which is not a supported operator against SKs so that design wouldn't work.

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