我有 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/