MongoDB:保存文档会重写整个文档吗?

标签 mongodb

我有 domain 集合,其中包含包含域信息的文档。其中一部分是历史 whois 记录,它可能是零个或多个,并且到目前为止占据了文档的大部分空间。

如果我加载整个文档,更改一些小的内容(例如更新数字字段)并使用 save() 方法将 mongo 将整个文档刷新到磁盘或仅更新已更改的 BSON ?最终我的问题是,我应该费心用 update() 使我的代码复杂化以节省 I/O 还是应该只使用 save()

这不仅仅是因为懒惰,文档(在完整阅读之后)会经过一系列步骤来修改/处理文档,如果进行了任何更改,则保存整个文档。但是如果保存文档的成本很高,那么也许我必须换一种方式考虑......

最佳答案

您可以使用 $set 更新文档中的单个字段.这将修改磁盘上的字段。但是,if the document grows beyond the size before the update, the document may have to be relocated on disk .

同时,here is what the documentation says about save versus update :

>// x is some JSON style object
>db.mycollection.save(x); // updates if exists; inserts if new
>
>// equivalent to:
>db.mycollection.update( { _id: x._id }, x, /*upsert*/ true );

引用文献

关于MongoDB:保存文档会重写整个文档吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182850/

相关文章:

mongodb - 使用 Kotlin 在 Mongo 中实现多个 Collection 的通用 getter

javascript - 小鹿交易遗失的藏品

javascript - Mongoose 加密

javascript - 在 Mongoose 中创建多对多关系,填充不是函数

node.js - 仅在 mongoose 中的填充数组中删除文档 objectid

python - 从 MongoDB 获取嵌套数据到 Pandas 数据框

mysql - 记录大量印象数据(5000 万条记录/月)

java - @Autowired 与 @Autowired 与 Setter

python - 插入 Mongo 时如何处理 json 中的长整型?

蒙戈数据库 : find documents including references to sub-documents [node-mongodb-native]