I am trying to make a function so that users can like posts (either a post is liked by the user or it is not liked by the user, i.e. no dislike or voting).
My posts are objects in a MongoDB collection with schema
{
title: String,
text: String
}
while my users have schema
{
username: String,
password: String
}
I could, for instance, create an array at posts with the ids of the users that have liked the specific post. It would look something like
{
title: String,
text: String,
likedByUsers: [ObjectID]
}
or I could create an array at users with the ids of the posts that the user has liked, which would be something like
{
username: String,
password: String,
postsLiked: [ObjectID]
}
However, I expect that the users will like thousands of posts, so I might face a restriction in the size of the objects in MongoDB.
So I am thinking that I should instead create a new collection with likes
{
userId: ObjectID,
postId: ObjectID
}
However, I am thinking how I should retrieve it in my app.
Should I store an array of the IDs of all the posts that the user (who is logged in) has liked, and when I am printing each post, I will look if the post is among the posts that the user has liked, and then show an "already liked" button?
Alternatively, I could send the user id when I am requesting to see all the posts, and then for each post add a field saying "isLiked", representing whether an object in the Liked collection could be find, matching both the post and the user id?