猫鼬,更新()与保存()
save()
和 update()
哪个更有效地更新文档的一部分?
Which is more efficient for updating parts of a document, save()
or update()
?
让这是一个 Schema
的例子:
Let this be an example of a Schema
:
var todoSchema = new mongoose.Schema({
name: String,
subtasks: Array
});
如果我 push()
- 通过常规的 JS 数组方法,将一些 subtasks
放入其中,然后调用 save()
,将猫鼬发送整个 todo
文档到数据库服务器还是只是添加的subtasks
?
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?
我的印象是使用 update()
到 $push
一个子任务,它只是直接在数据库上执行操作,仅通过线路发送 $push
-ed 子任务,而不是 update()
,后者只会通过线路发送 subtask
.>
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 can actually see what gets sent to be saved when you have debugging enabled:
mongoose.set('debug', true);
当我使用 save()
时,它只发送要保存的更改内容.
And when I've worked with save()
it only sends what was changed to be saved.