Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT FOR UPDATE SKIP LOCKED in MongoDB

Tags:

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?

like image 217
silent-box Avatar asked Jun 26 '18 11:06

silent-box


1 Answers

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.

like image 131
Radosław Miernik Avatar answered Oct 04 '22 20:10

Radosław Miernik