findOneAndUpdate 和 findOneAndReplace 之间有什么区别? [英] What's the difference between findOneAndUpdate and findOneAndReplace?
问题描述
我最近安装了 Java MongoDB Driver 3.1.1 版本,我想知道 findOneAndUpdate
和 findOneAndReplace
之间有什么区别?
I've recently installed the Java MongoDB Driver 3.1.1 version and I'm wondering what's the difference between findOneAndUpdate
and findOneAndReplace
?
我应该在什么情况下使用每一种?
In what situation should i use each one?
推荐答案
findOneAndUpdate
搜索文档并仅更新给定更新文档中的条目.找到的文档中的其他条目将保留.
The findOneAndUpdate
searches the document and updates just the entries in the given update document. The other entries in the found document will remain.
findOneAndReplace
搜索文档,删除此文档中的所有内容,并设置给定替换文档的条目.
The findOneAndReplace
searches the document, removes everything inside this document and sets the entries of the given replacement document.
例如:你有一个文档 {"name":"James", "age":"21"}
如果您对更新文档{"age":"22"}
使用findOneAndUpdate
函数,您将获得文档{"name":詹姆斯",年龄":22"}
If you use the findOneAndUpdate
function with the update document {"age":"22"}
, you will get the document {"name":"James", "age":"22"}
如果您对替换文档{"age":"22"}
使用findOneAndReplace
函数,您将获得文档{"age":"22"}
(名字已被删除)
If you use the findOneAndReplace
function with the replacement document {"age":"22"}
, you will get the document {"age":"22"}
(The name has been deleted)
请参阅:findOneAndUpdate 文档 和 findOneAndReplace 文档
这篇关于findOneAndUpdate 和 findOneAndReplace 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!