Why is save() recommended over update methods? #13221
-
|
Hello, The mongoose documentation states: So mongoose says: _You should use save() rather than updateOne() and updateMany() where possible. However, Model.updateOne() and Model.updateMany() have a few advantages: updateOne() is atomic. If you load a document using find(), it may change before you save() it. The docs don't explain why it is recommended to use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
We'll add a little more info on that. Because you have the document in memory, |
Beta Was this translation helpful? Give feedback.
-
|
@vkarpov15 I think Also, can you elaborate (or point me to documentation) why having validators on |
Beta Was this translation helpful? Give feedback.
We'll add a little more info on that. Because you have the document in memory,
save()gives you more complete validation (updateOne()can't validateinc, etc.) and better middleware support. Sosave()gives you access to more features, butupdateOne()is faster and atomic.