1 / 1
Nov 2024

Hello, I have some questions regarding UpdateOne and InsertOne behavior when performed in the same unordered BulkWrite operation or separately in 2 BulkWrites but executed at the exact same time.

For example, if I perform the 2 operations below in a single unordered BulkWrite:

db.collection.insertOne( { "_id": "A", "status": "in_progress", "...": "..." } )
db.collection.updateOne( { "_id": "A" }, { "_id": "A", "status": "success", "...": "..." }, { upsert: true } )

I would assume that, after execution, the resulting document would always have a status of success. However, after load testing, I still find some documents with an in_progress status.

My questions are:

  1. What is causing this behavior? Looking at the operation, I would assume that either the insert is performed first and would then be overridden by the update or the insert is performed second which would throw a duplicate key exception due to the unique index _id.

  2. Is there any way to guarantee that the data in the UpdateOne would always be the one persisted in the resulting document?

I would appreciate any help. Thank you.