HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

mongodb bulk write all or nothing?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
allmongodbbulkwritenothing

Problem

I am learning about mongodb. If I create a bulk write is this transaction all or nothing? I have a scenario where my users can delete who they are following. For this to happen I need to delete from user A who they are following and from User B the follower. For consistency I need these to occur as a all or nothing because I wouldn't want only 1 of the 2 operations to succeed as this would cause bad data. Reading the docs:

https://docs.mongodb.org/manual/core/bulk-write-operations/

I could not find the answer.

Solution

MongoDB transactions are always atomic on single document. However, if it involves multiple documents transactions are not atomic.

Coming to Bulk operations, there is no transactions concept. For example, you are inserting 100 documents in a collection, if 51st insert fails, MongoDB will not insert the remaining documents in the list as it executes the bulk operations serially.

Context

StackExchange Database Administrators Q#122992, answer score: 3

Revisions (0)

No revisions yet.