findOneAndUpdate 和 findOneAndReplace 之间有什么区别? [英] What's the difference between findOneAndUpdate and findOneAndReplace?

查看:196
本文介绍了findOneAndUpdate 和 findOneAndReplace 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了 Java MongoDB Driver 3.1.1 版本,我想知道 findOneAndUpdatefindOneAndReplace 之间有什么区别?

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屋!

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