Wait for pessimistic "lock" to release


There is this article:
How To SELECT … FOR UPDATE inside MongoDB Transactions

that explains how pessimistic locks could be implemented in MongoDB. This is somewhat equivalent to the SELECT FOR UPDATE NOWAIT in PostgreSQL.

However, I’m more interested in the default behavior of the SELECT FOR UPDATE statement (skipping NOWAIT clause) that actually waits for the lock to release.

The approach explained in the aforementioned article will actually lead to an error being thrown when the document can’t be locked and this event not only will break the findOneAndUpdate() query, but it will fail the entire transaction, so it can’t be reused and must be started from the beginning.

Is it possible to wait for the lock to release inside the transaction without having to start it from the beginning if one of the documents can’t be locked?

Thank you.