Data structure for like/favorite in social blogging app

Viewed 26

What's the best way to save data in db to keep track of a user clicked a like/favorite button or not?

I've tried the following data structure:

postId:{ ... like_count:123, user_who_liked:[uid1,uid2,uid3...] }

When the data above is downloaded to the client, we can check if the client liked the post or not by checking if the array user_who_liked contains the client's uid or not. However, if there are more than 1000 items in the user_who_liked array, this approach consumes too much redundant bandwidth.

I found the following json data from instagram:

{ ... viewer_has_liked: false viewer_has_saved: false viewer_has_saved_to_collection: false viewer_in_photo_of_you: false }

This approach seems more efficient, but how do the database know if the user has liked or not? Do they store a bunch of uids and check if the user's uid is inside it? Are those has_liked/has_saved booleans derived attributes?

0 Answers
Related