Read locks in transactions

Hello,

When working with transactions, how do I achieve lock the reads as well?

I am working with a method that replaces the document (using UpdateOneAndAsync does not work in this case), so I need to use transactions to achieve atomicity and not allow multiple writes. This works fine, but I notice that the reads are not locked, meaning that I am able to read the document that I have lock-in another transaction, is there a way to lock the read as well?

Regards

Hi @Rebeca_M welcome to the community!

When working with transactions, how do I achieve lock the reads as well?

In short, I don’t think you can. Transactions mainly concerns document writes and not presenting a state of the database that is not consistent, not really about preventing reads. Preventing readers would likely have a negative effect on your database’s parallelism and performance.

However, do you mind elaborating more if you have a specific use case in mind that requires you to prevent reads? Some example scenarios would be helpful to illustrate the point.

Best regards
Kevin

Hey @kevinadi , I’m looking for the same kind of read locks, my use case is that of creating a history/audit log, which has to be in the order of updates to the same document.

Hi,
This document artilce will describe the “isolation” level used in MongoDB “Read Uncommitted”: https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency/

Regards,
Emad

this is not a lock on reads. it only decides what data you can read as a result.

I think in this case, Mongodb doesn’t have something similar to “select xxxx for share”