PyMongo alternative to findAndModify

Viewed 58

Background: Python 3.7, Mongo 4.4, Ubuntu 20.04

I want to add a sequence number (unique integer) to new documents in my application. I'm holding a counter in the DB and using it as the unique number, increasing it after each new document.

Since my application is multi-processed, I sync this counter between processes using pyMongo findAndModify function, which is atomic (see Atomicity and Transactions).

Here is a simplified example of the code:

from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client['data']
collection = db.counters

query = {'_id' = 'sequence_data'}
update = {'$inc':{ 'counter':1}}

updated_doc = collection.find_and_modify(query, update)
return updated_doc['counter']

Problem: The findAndModify function is deprecated (see PyMongo 4.0 changelog), and following the guidelines in PyMongo 4 Migration Guide, i tried to use the find_one_and_update function, but it is not atomic for the find part.

This can cause a race condition between 2 processes that execute the find_one_and_update and both read the same counter value, before incrementing it.

Question: So my basic question is, how do I apply atomicity on the read section of my logic between multiple processes/threads?

0 Answers
Related