I am using Parse Server, which runs on MongoDB.
Let's say I have collections User
and Comment
and a join table of user and comment.
User can like a comment, which creates a new record in a join table.
Specifically in Parse Server, join table can be defined using a 'relation' field in the collection.
Now when I want to retrieve all comments, I also need to know, whether each of them is liked by the current user. How can I do this, without doing additional queries?
You might say I could create an array field likers
in Comment
table and use $elemMatch
, but it doesn't seem as a good idea, because potentially, there can be thousands of likes on a comment.
My idea, but I hope there could be a better solution:
I could create an array field someLikers
, a relation (join table) field allLikers
and a number field likesCount
in Comment
table. Then put first 100 likers in both someLikers
and allLikers
and additional likers only in the allLikers
. I would always increment the likesCount
.
Then when querying a list of comments, I would implement the call with $elemMatch
, which would tell me whether the current user is inside someLikers
. When I would get the comments, I would check whether some of the comments have likesCount > 100
AND $elemMatch
returned null. If so, I would have to run another query in the join table, looking for those comments and checking (querying by) whether they are liked by the current user.
Is there a better option?
Thanks!