Reputation: 11
I'm building a simple application where posts appear on a user's home page and he can like or unlike them. first when there were no users in the applications, I made a simple boolean field "liked", as shown in the figure, to determine if a post is liked or un-liked. However, when I started working with users, I find it hard to find the perfect structure for the likes field.
In Firestore, I added a field named "likedBy" for each post, which contains a map with a key of each user's id and a boolean to determine if the user liked the post or not.
I don't know if this structure is suitable or not, and if not, is there a better way to reach my goal?
Upvotes: 1
Views: 603
Reputation: 8383
I would recommend going one step further and save more than the User ID. You could also save the User displayName
and photoUrl
. That way, you don't need to read ($$$$money$$$$) the documents of every Users
who liked Posts
in order to display their name and/or avatar.
Upvotes: 1
Reputation: 2982
The likedBy
field is enough to cover most use cases.
Just store in likedBy
an array of all users who liked the post by user ID. When a user likes - add the user ID to the array and remove it upon unlike.
That means you can also remove the likes
and liked
field as you can get it from reading the likedBy
array.
Upvotes: 1