Schema:
Schema({
...
review: [],
count: {type: Number, default: 0},
average: {type: Number, default: 0},
...
});
Count and average is the number of reviews and the average rating respectively. Review is an array of document:
Schema({
...
text: String,
rating: Number,
...
});
When saving and removing Review
, $inc operator is used
{$inc:{count:1}} //saving
{$inc:{count:-11}} //removing
How to update the average
when a new review is added, updated and deleted? Is is possible in update query?
THanks