Does a transaction lock a document from being found or from being written to?

Hello community,
I’m building a demo app that involves some dummy wallet transactions. In this app, I want to properly handle race conditions, ensuring that no wallet balance ever drops below zero (or a set minimum balance). To achieve this, I’m using the $inc operator within transactions.

My question is: if a wallet document is locked by a findOneAndUpdate operation inside a transaction (because its balance is being updated), will another findOneAndUpdate be blocked from even finding the document until the first transaction completes? And if so, does the find query need to reflect the most up-to-date state of the document once the lock is released? Or will it be able to find the document immediately but have to wait for the lock before applying the update?

I want to properly handle race conditions, ensuring that no wallet balance ever drops below zero (or a set minimum balance). To achieve this, I’m using the $inc operator within transactions.

You don’t need a transaction for this. A single update is 100% atomic and transactional - there is no extra benefit to wrapping it in a transaction.

You also don’t need findOneAndUpdate when you just need to update - you only need it if you have to get back the updated document.

But separately from your example which doesn’t need transactions, if a running transaction has updated a document, this document is still available in the current snapshot to be found by other read operations, since it has not yet been committed.

Asya