Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.