skip to Main Content

Here i am trying to update the ‘rating’ field of my document by taking average of previously existing value of the rating field and newly sent rating value.

this is my rating field specification in the model

rating: {
      type: Number,
      min: 0,
      max: 5,
      required: true,
},

this is my request body and controller function

const { newRating, bookID, userName, comment } = req.body;

const updateRating = await Book.findByIdAndUpdate(
    { _id: bookID },
    {
      rating: { $divide: [{ $inc: { rating: Number(newRating) } }, 2] },
      $inc: { numOfRatings: 1 },
    },
    { new: true }
    );

and i am using postman to send client side data

here for example the rating field has previously set value of 4.1 and i am sending 5 as new rating in req.body then i want the rating field to have an updated value of 4.55 ((4.1+5)/2)

and this is the output i am getting in postman

{
"message": "Cast to Number failed for value "{ ‘$divide’: [ { ‘$inc’: [Object] }, 2 ] }" (type Object) at path "rating"",
"stack": "CastError: Cast to Number failed for value "{ ‘$divide’: [ { ‘$inc’: [Object] }, 2 ] }" (type Object) at path "rating"n at model.Query.exec (D:ProgramsVS CodeWeb Developmentlmsbackendnode_modulesmongooselibquery.js:4891:21)n at model.Query.Query.then (D:ProgramsVS CodeWeb Developmentlmsbackendnode_modulesmongooselibquery.js:4990:15)n at processTicksAndRejections (node:internal/process/task_queues:96:5)"
}

i tried few things seeing mongodb solutions but it is not working out for me. Thank you in advance.

2

Answers


  1. $divide is only available for Aggregation framework, so you need to change your update (second) input like this:

    await Book.findByIdAndUpdate({
      _id: bookID
    },
    [
      {
        "$set": {
          "rating": { "$divide": [{ "$sum": ["$rating", Number(newRating)] }, 2] },
          "numOfRatings": { "$sum": ["$numOfRatings", 1 ] }
        }
      }
    ])
    

    Working example

    Login or Signup to reply.
  2. I hope this will work:

    const updateRating = await Book.findByIdAndUpdate(
      { _id: bookID },
      [{
        "$set": {
          "rating": { "$divide": [{ "$sum": ["$rating", 5] }, 2] },
          "numOfRatings": { "$sum": ["$numOfRatings", 1 ] }
        }
      }],
      { new: true }
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search