The updateMany takes in a query filter. Suppose this query filter returns 5 documents to update. Then this operation will try to update these 5 documents. If another thread (in the meantime, after this updateMany's query filter has run and returned 5 documents for it to update ):
Updates one or more of these 5 documents then when this updateMany tries to update there would be an error returned?
Deletes one of these documents then when this updateMany tries to update there would be an error returned?
Inserts another record that matches the query filter of this updateMany then that record would not be updated by this operation?
MongoDB regular updates like updateMany only assure atomicity on a single document level.
This means that an operation will lock one document at a time and update it while others wait for the lock.
However, updateMany may get a document and before it changes it another thread will take the lock and change it. Any operations after that will overwrite the previous thread and should not error out, only if a write timeout is exceeded but this does not guarantee that the write will rollback.
If you require multiple document snapshot isolation so that only one thread can change those you need to use transactions with your flow.
As far as I understand without a transaction the updateMany scans the document and when it finds a relevant doc it locks it. There is a findAndUpdate commands where you can atomically query values and only than update them… So maybe you can add a flag of "in-batch’ for others to not even try this update
During this time only this thread can update that single document and other wait in line for the query. Any other documents even those who were scanned and altered can be read and updated by other threads.
In a transaction all of the records that already updated are locked until commit. Operations outside of the transactions will error out.