I have a collection that stores products
.
Each product
can be rated by users.
There will be another collection to hold the ratings
of products
by users.
In the products
collection, there will be a field to store the average of the last N rating.
The ratings
collection will be indexed by date added, of course.
-
option 1 - when a new rating is added for a product, go to the
ratings
collection, retrieve the last N ratings, compute the average and then replace the current average in theproducts
collection. -
option 2 - store an array of the last N ratings in a field of the
products
collection. When a new rating is added, push this into this array, pop out the oldest, and then compute the average from this array.
- Assuming that N is small, what is the trade off between the two options above?
- Assuming that N is large, is option 2 still viable?