Reputation: 39
I am trying to implement in LIKE and COMMENT system like Instagram and Facebook in DynamoDB
I have 3 tables, user, photo and photo_likes
user table and photo table have user_id and photo_id keys.
and for photo_likes table, I have photo_id as a key and liked_by column where I store user_id as list.
So, If the photo_id 1 is liked by user_id 10, 35, I store it as:
| photo_id | liked_by |
| 1 | {10,35} |
I am really confused if it is the right way to do it? or should I just insert a new row whenever their is a new like.
| photo_id | user_id |
| 1 | 10 |
| 1 | 35 |
Upvotes: 0
Views: 1499
Reputation: 39216
I will provide the advantage and disadvantage of the above approaches. You may need to choose the correct approach based on your Query Access Patterns required for your application.
Approach 1:
| photo_id | liked_by |
| 1 | {10,35} |
Advantage & Suggestion:-
liked_by
as NS
or SS
rather than List. So that it will not have duplicates.liked_by
are present in the same item. This will help during retrieval and showing the results on GUI if requiredDisadvantage:-
Approach 2:
| photo_id | user_id |
| 1 | 10 |
| 1 | 35 |
You can define photo_id
as partition key and user_id
as sort key.
Advantage:-
Disadvantage:-
likes
should be done at client side by iterating the values. In approach 1, you can use some array.length
to get the likes
countlikes
count or any similar sceanrioUpvotes: 1