How to use Mongoose findOneAndUpdate

Mongoose provides various methods to update the document like .save(), findOneAndUpdate(), updateOne(). In today’s post we are going to use findOneAndUpdate(). findOneAndUpdate(): There are multiple…

The post How to use Mongoose findOneAndUpdate appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

Mongoose provides various methods to update the document like .save(), findOneAndUpdate(), updateOne(). In today’s post we are going to use findOneAndUpdate().

findOneAndUpdate():

There are multiple documents saved inside mongoDB which match the criteria of update condition. findOneAndUpdate method matches the first document that satisfies the condition & updates it. By default, findOneAndUpdate() returns the original document as it was before update method was applied.


const filter = { name: 'John Doe' };
const update = { age: 30 };

const oldDocument = await User.findOneAndUpdate(filter, update);
oldDocument.name; // 'John Doe'
oldDocument.age; // 29

const updatedDocument = await User.findOneAndUpdate(filter, update, {
  new: true
});

updatedDocument.name; // 'John Doe'
updatedDocument.age; // 30

If you want the updated document to be written just pass  new options set to true. returnOriginal is another parameter that identifies whether an updated document will be written or not. If you set returnOriginal: false it will return the updated document.

Difference between .save() & findOneAndUpdate():

The main difference between these two method is findOneAndUpdate is atomic in nature which means the document does not change between MongoDB finding & updating the document. On the flip side .save() does not guarantee atomic update.

The post How to use Mongoose findOneAndUpdate appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-10-15T12:15:11+00:00) How to use Mongoose findOneAndUpdate. Retrieved from https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/

MLA
" » How to use Mongoose findOneAndUpdate." Deven | Sciencx - Friday October 15, 2021, https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/
HARVARD
Deven | Sciencx Friday October 15, 2021 » How to use Mongoose findOneAndUpdate., viewed ,<https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/>
VANCOUVER
Deven | Sciencx - » How to use Mongoose findOneAndUpdate. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/
CHICAGO
" » How to use Mongoose findOneAndUpdate." Deven | Sciencx - Accessed . https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/
IEEE
" » How to use Mongoose findOneAndUpdate." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/. [Accessed: ]
rf:citation
» How to use Mongoose findOneAndUpdate | Deven | Sciencx | https://www.scien.cx/2021/10/15/how-to-use-mongoose-findoneandupdate/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.