0

i'm using findOneAndUpdate function from mongoose and in the nodejs i am updating a record now i want to send the updated data in response but as mongoose stop supporting callback function in updates now i have to do two request to mongo

first one to update the data and the another one to get the updated record ... like this

module.exports.addcontact = async (req, res) => {

  await UserModel.findOneAndUpdate(
        { email: email },
        { $set: { number : req.bode.number } },
      );
  
 const data = await UserModel.findOne({ email: decode.email });
 res.send({ stat: true, decode, wallets: data.Wallets });
}

Here first i'm updating the record and after that again finding it and storing in data variable to get the latest record after the updates made..

i want to know if it is possible to update the data and get the updated document in one time so dont have to apply multiple operation for one task only.

Geekyvinayak
  • 197
  • 7
  • 2
    Use `{ new: true }` or `{ returnOriginal: false }` in `findOneAndUpdate()` – Shri Hari L Jul 13 '23 at 05:26
  • 2
    See the official [doc](https://mongoosejs.com/docs/tutorials/findoneandupdate.html#getting-started). It already saying `You should set the new option to true to return the document after update was applied.` – PRATHEESH PC Jul 13 '23 at 05:28

0 Answers0