updateMany query filter and update are atomic?

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 ):

  1. Updates one or more of these 5 documents then when this updateMany tries to update there would be an error returned?
  2. Deletes one of these documents then when this updateMany tries to update there would be an error returned?
  3. Inserts another record that matches the query filter of this updateMany then that record would not be updated by this operation?

IS my understanding correct?

db.collection.updateMany(filter, update, options)

1 Like

Hi @Gaurav_Khanna ,

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.

Thanks
Pavel

Thanks Pavel. However, I think I was not clear. Please see the questions. I am worried about the race condition between the query and the update part of the updateMany() operation.

We can answer these in terms of when updateMany() operation is running in a transaction and when it is not. The other threads might or might not be in a transaction.

I read the documents that you linked to and could not find a definitive answer on these.

Hi @Gaurav_Khanna ,

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.

Thanks
Pavel

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.