I have 100+ worker threads, which are going to poll database, looking for a new job.
To take a job, a thread need to change status of the bunch of documents from NEW
to IN_PROGRESS
, so no other threads can peek the same job.
This can be solved perfectly fine in PostgreSQL with SELECT FOR UPDATE SKIP LOCKED WHERE status = "NEW"
statement.
Is there a way to do such atomic update in MongoDB for a single document? For a batch?
There's a findAndModify
method, which works exactly as you've described for a single document.
For a batch, it's not possible right now, as
In MongoDB, write operations, e.g.
db.collection.update()
,db.collection.findAndModify()
,db.collection.remove()
, are atomic on the level of a single document.
It will be possible in MongoDB 4.0 though, with transactions.
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