Skip to content

Commit

Permalink
Creating pipeline to calculate and display average ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
Safar123 committed May 30, 2024
1 parent d195a16 commit 4e6c1e7
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Model/reviewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ const reviewSchema = new mongoose.Schema({

});

reviewSchema.index({recipe:1, user:1}, {unique:true});

reviewSchema.statics.calcAverageRating = async function(recipeId){
console.log(recipeId);
const stat = await this.aggregate([
{
$match:{recipe: recipeId}
$match: {recipe: recipeId}
},
{
$group: {
Expand All @@ -49,16 +51,14 @@ reviewSchema.statics.calcAverageRating = async function(recipeId){
}

])

await Recipe.findByIdAndUpdate(recipeId, {
numberOfRatings:stat[0].nRating,
averageRating:stat[1].avgRating
averageRating:stat[0].avgRating
})
}


reviewSchema.post('save', function(){

this.constructor.calcAverageRating(this.recipe);
})

Expand Down

0 comments on commit 4e6c1e7

Please sign in to comment.