Here are the references for:
find_one_and_update
methodFinds a single document and updates it, returning either the original or the updated document.
update_one
methodUpdate a single document matching the filter.
It seems to me both of them first query the selected document, then update it.
I would like to know if there is some kind of optimization difference. The only other difference is in the return value, which makes me wonder why one would even use update_one()
at all.
There are some changes between that two operations:
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.
So this method do a kind of find
operation that allows you to sort and filter records in your database.
update_one
With this method you can't sort your records, it just does a find
operation and update each found elements with a for
cycle.
So at the end i think the update_one
operation is faster than the find_one_and_update
operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With