mongodb findoneandupdate return updated
mongodb findoneandupdate return updated

Hi Guys, Welcome to Proto Coders Point. When you execult findOneAndUpdate by default mongodb return an unaltered data/document i.e. The query will the original data before updating. Mongodb findOneAndUpdate return old data.

How to Mongodb query findOneAndUpdate return updated data

I want mongodb findOneAndUpdate to return updated value.

If you want to newly updated document returned then you need to just make use of additional argument in the query i.e. A Object with new property and set it to true.

Solution

Just pass {new: true}, and this will return the updated document of mongodb findOneAndUpdate query.

Eg:

let updateStatus = await Model.findOneAndUpdate({ _id: id }, { status }, { new: true });
console.log(updateStatus);