mongodb - PyMongo for MongoDB 中 find_one_and_update() 和 update_one() 之间的区别?

标签 mongodb pymongo

以下是引用资料:

  • find_one_and_update 方法

  • Finds a single document and updates it, returning either the original or the updated document.


  • update_one 方法

  • Update a single document matching the filter.



    在我看来,他们都首先查询所选文档,然后更新它。

    我想知道是否存在某种优化差异。唯一的另一个区别是返回值,这让我想知道为什么甚至会使用 update_one()根本。

    最佳答案

    这两个操作之间有一些变化:

  • find_one_and_update

  • By default :meth:find_one_and_update returns the original version of the document before the update was applied. To return the updated version of the document instead, use the return_document option.

    You can limit the fields returned with the projection option.

    The upsert option can be used to create the document if it doesn't already exist.

    If multiple documents match filter, a sort can be applied.



    所以这个方法做了一种find允许您对数据库中的记录进行排序和过滤的操作。
  • update_one

  • 用这种方法你不能对你的记录进行排序,它只是做一个 find操作并使用 for 更新每个找到的元素循环。

    所以最后我认为 update_one操作比find_one_and_update快手术。

    关于mongodb - PyMongo for MongoDB 中 find_one_and_update() 和 update_one() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51989023/

    相关文章:

    python - 导入错误 : No module named parse

    mongodb - 仅返回 MongoDB 中的子文档

    mongodb - Golang使用MongoDB报告 "context deadline exceeded"

    mongodb - 如何将排序后的数组存储回 MongoDB?

    mongodb - 使用 mapReduce 选择一行中的所有字段

    python - 在 Python 中使用 JSON 将查询发送到 MongoDB

    mongodb - Ionic 框架应用程序离线工作并与 mongoDB 同步数据

    python - 将带有值的字段添加到 mongo 返回的文档中

    python - pymongo 数据库的 insert() 和 insert_one() 方法都不能调用“Collection”对象

    python - 如何在Python脚本中编写查询