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

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

问题描述

findOneAndUpdate和update有什么区别? 两者都接受查询条件和文档进行更新.

What is difference of 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和更新的猫鼬区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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