UpdateMany Behavior With Multiple Updates To Same Document

I understand that with other operations such as insertMany there’s an option for ordered meaning that the inserts are performed in the order specified but its unclear how the update processes behaves. Especially in the event that more than one update to the same document is part of the list of updates.

Is it possible that one update wins over the other every time? Or would it be seemingly random?

We’re running into an issue where we have multiple updates being applied to documents, with a fix soon coming to address that. I was just unsure how Mongo itself would handle those updates assuming one or more documents are being updated multiple times during the updateMany command.

What I would assume would be the case is that one update might be processed before the other (assuming they aren’t ordered) and then the update that modifies the document LAST is the state of the document going forward. Essentially “overwriting” the update from before (we aren’t doing $addToSet or anything - just property values) so a change to property x to value y might be changed to value z.

Am I on the right track here?

Hi @Wyatt_Baggett

Welcome to MongoDB community.

You are on the right track so each update is atomic but in a batch the last update wins.

However, it will affect locking performance and result in increased writeConflicts witch will slow your write rate.

You might be able to control the updates better with bulk.find().updateOne() updating values only once per document. To do this build the array command during your processing and execute when a definitive unique amount of updates accumulated…

Thanks
Pavel

1 Like

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