以下是引用资料:
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/