I couldn't find any documentation stating that find_one_and_update()
in pymongo
is atomic (i.e., no change on the document can appear between search and update). Can I rely on it being atomic? Or is there another way to perform the same operations in a guaranteed atomic way?
PyMongo's find_one_and_update
is atomic as it translates to server's findOneAndUpdate
which is atomic (https://docs.mongodb.com/manual/core/write-operations-atomicity/#atomicity).
However, be aware of the "glitch" when upserting new documents. When not using a unique index on the query field(s), multiple instances of a findOneAndUpdate
operation with similar query field(s) could result in duplicate documents being inserted in certain circumstances. (https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/#upsert-with-unique-index)