UpdateOne with upsert on two threads throws MongoDuplicateKeyException

Hello @James_Kovacs

Thanks for your answer.

If I understand your suggestion correctly, I would have to perform an upsert with the required id to make sure that the document exists, before I perform the update with the combined filter.
That would lead to two steps each time I want to update a document.

Therefore I think, the most efficient way would be to perform the upsert with the combined filter and, when the MongoDuplicateKeyException is thrown, repeat the same operation, which will then perform a successful update.
With this approach there should be only two steps in the very few cases, when two threads perform the initial upsert at the same time.
In all other cases it will have only one step.

Sincerely,
Andreas