Star rating for a doc


#1

Hey guys, I would like to have some kind of review for a document. I understand how to make it for the front but from the back end. How can I have a rating in the doc, I mean do I need to create a empty array who receive the rating ?


#2

I would probably track for each user what docs they rated and the score they gave. You can then just have a field in the doc that represents it’s average rating and another to track how many total ratings it has, each time a user submits a rating the average and count gets updated. To calculate the average you also track the sum of all ratings and divide by the total count.

When a user rates a doc, you look up to see if they have already rated the doc so you can undo their old rating first before applying the new one. Don’t increment the number of ratings if they are just changing their existing rating.