Mongoose difference between .save() and using update()
Two concepts first. Your application is the Client, Mongodb is the Server.
The main difference is that with .save()
you already have an object in your client side code or had to retrieve the data from the server before you are writing it back, and you are writing back the whole thing.
On the other hand .update()
does not require the data to be loaded to the client from the server. All of the interaction happens server side without retrieving to the client.So .update()
can be very efficient in this way when you are adding content to existing documents.
In addition, there is the multi
parameter to .update()
that allows the actions to be performed on more than one document that matches the query condition.
There are some things in convenience methods that you lose when using .update()
as a call, but the benefits for certain operations is the "trade-off" you have to bear. For more information on this, and the options available, see the documentation.
In short .save()
is a client side interface, .update()
is server side.
Some differences:
- As noted elsewhere,
update
is more efficient thanfind
followed bysave
because it avoids loading the whole document. - A Mongoose
update
translates into a MongoDBupdate
but a Mongoosesave
is converted into either a MongoDBinsert
(for a new document) or anupdate
. - It's important to note that on
save
, Mongoose internally diffs the document and only sends the fields that have actually changed. This is good for atomicity. - By default validation is not run on
update
but it can be enabled. - The middleware API (
pre
andpost
hooks) is different.