猫鼬,更新()与保存() [英] Mongoose, update() vs save()

查看:56
本文介绍了猫鼬,更新()与保存()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于猫鼬,更新()与保存()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆