title: mongoose and nodeJs for update query
body: <p>i am new to nodejs and mongoDB</p>
  <p>i am developing a project in to it i need to calculate rating on product for this i've done
  in to model</p>
  <pre><code>    ReviewSchema.statics.calculateAverage = async function (productId) {
            console.log(&quot;in to calculate rating &quot; + productId);
            const result = await this.aggregate([
              { $match: { product: productId } },
              {
                $group: {
                  _id: null,
                  avgRating: { $avg: &quot;$rating&quot; },
                  numberOfRating: { $sum: 1 },
                },
              },
            ]);
            // this.model('Product').findOneAndUpdate({_id:productId});
            console.log(&quot;resulr....&quot; + result[0].avgRating);
          
            await this.model(&quot;Product&quot;).findOneAndUpdate(
              { _id: productId },
              {
                // averageRating: Math.ceil(result[0]?.avgRating || 0),//working but want fraction
                averageRating: result[0]?.avgRating || 0,
                numOfReviews: result[0]?.numberOfRating || 0,
              }
            );
          };
          
          //for save
          ReviewSchema.post(&quot;save&quot;, async function () {
            await this.constructor.calculateAverage(this.product);
          });
  //for remove
          ReviewSchema.post(&quot;remove&quot;, async function () {
        await this.constructor.calculateAverage(this.product);
      });
  </code></pre>
  <p>this works ok on saving or removing review i.e calculation of review working perfectly
  Now, my query is when user update review i want to recalculate the reviews
  for that i've done</p>
  <pre><code>ReviewSchema.post(&quot;findOneAndUpdate&quot;, async function () {
    console.log(&quot;test....update&quot;);
  
    await this.constructor.calculateAverage(this.product);// this line gives me this.constructor.calculateAverage is not a function
   
  });
  </code></pre>
  <p>but for update it gives me <strong>this.constructor.calculateAverage is not a function</strong></p>
  <p>please help me to solve this issue</p>
  <p>(for update if i do use save() then works ok but i want solution with findOneAndUpdate if it possible please guide me )</p>