findOneAndUpdate 和 update 的猫鼬区别 [英] mongoose difference of findOneAndUpdate and update

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

问题描述

findOneAndUpdate 和 update 有什么区别?

What is the difference between findOneAndUpdate and update?

都接受要查询的条件和要更新的文档.

Both accept criteria to query and doc to update.

推荐答案

好吧, 都有各自的文档可供查看.update().findAndModify() 这是这里 .findOneAndUpdate() 的根方法.

Well there is the respective documentation to view for both .update() and .findAndModify() which is the root method of .findOneAndUpdate() here.

但主要区别在于:

  • update():旨在对集合中与其查询条件匹配的一个或多个"文档执行原子更新操作.它在响应中返回已修改文档的数量.

  • update(): Is meant to perform an atomic update operation against "one or more" documents matched by it's query condition in a collection. It returns the number of modified documents in it's response.

findOneAndUpdate():用于处理单一"文档的更新语句,以及检索该单一"文档的内容.返回的状态取决于传递给操作的新"选项的值.其中 true 返回修改过的"文档.其中 false 在任何修改之前返回原始"文档.后一种形式是默认选项.

findOneAndUpdate(): Has the purpose of both processing an update statment on a "singular" document, as well as retrieving the content of that "singular" document. The state returned depends on the value of the "new" option as passed to the operation. Where true the "modified" document is returned. Where false the "original" document is returned before any modification. The latter form is the default option.

简而言之.一种是批量"修改,而不用担心结果文档内容.另一个是修改单个文档并返回文档内容结果.

In short. One is meant to modify in "bulk" and not worry with the document content in result. And the other is meant to modify a singular document and return the document content in result.

这就是区别.

这篇关于findOneAndUpdate 和 update 的猫鼬区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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