BulkWrite on single document - ACID

So, a little intro to my document schema.

My document consists of multiple properties, and one of them is an array of objects (let us call it prop1).
prop1 is being maintained by RabbitMQ system in a way that single message can add new objects to prop1, update existing objects, and even delete ones that are marked for deletion.

So, the way I made that happen is by using BulkWrite method, with IsOrdered=false option.
When a message is received, I parse it, and build a list of create, update and delete statements.

And then it is being executed. I would like to point out that all of that is happening on a single document.

What I am concerned about is is this usage of BulkWrite atomic? Should I wrap it in a transaction?

*NOTE:
I know that operations on a single document are ACID guaranteed, but I am in doubt because of BulkWrite, and the statement that states that BulkWrite may leave some operations undone in case of an error. I want to make sure that BulkWrite statements are executed in a All Or Nothing way).

Regards,
Marko Saravanja

Hello.

To make the BulkWrite operation atomic, use Transactions. Within a bulk write you can have write operations insertOne, updateOne, updateMany, etc., and these operations are individually atomic at a document level only.

If you are having multiple separate update operations on a single document, they are still multiple operations (each one atomic on its own). So, performing the bulk write within a transaction can solve your issue.

That said, if you have multiple updates/changes on a single document, and you want to apply them in a single operation, then you can use the db.collection.updateOne - and this would be atomic without using a Transaction. You have the option of using various update operators or the Update With Aggregation Pipeline.

1 Like

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