Nicholas Kyriakides Nicholas Kyriakides - 3 months ago 34
Node.js Question

Mongoose, update() vs save()

Which is more efficient for updating parts of a document,

save()
or
update()
?

Let this be an example of a
Schema
:

var todoSchema = new mongoose.Schema({
name: String,
subtasks: Array
});


If I
push()
- via regular JS array methods, some
subtasks
into it and then call
save()
, would mongoose send the whole
todo
document to the database server or just the
subtasks
that was added?

I'm under that impression that using
update()
to
$push
a subtask it simply performs the operation directly on the database, sending only the
$push
-ed subtask through the wire as opposed to
update()
which would just send the
subtask
over the wire.

You You
Answer

You can actually see what gets sent to be saved when you have debugging enabled:

mongoose.set('debug', true);

And when I've worked with save() it only sends what was changed to be saved.

Comments