Mongoose | findOneAndUpdate () Function. The findOneAndUpdate () function is used to find a matching document and update it according to the update arg, passing any options, and returns the found document (if any) to the callback.
Mongoose provides various methods to update the document like .save (), findOneAndUpdate (), updateOne (). In today’s post we are going to use fin dOneAndUpdate ().
Unlike updateOne (), findOneAndUpdate () returns the updated document. Unlike save (), findOneAndUpdate () is atomic: the document can't change between when MongoDB finds the document and when MongoDB applies the update. You need at least 2 parameters to call findOneAndUpdate (): filter and update.
By default, findOneAndUpdate () returns the original document as it was before update method was applied.