I have two collections - collection A and collection B. Collection A has a trigger which is invoked when a new record is inserted into this collection and updates a record in Collection B.
By default a transaction is mongodb is scoped to a document. My question is that if trigger on collection A and associated change in collection B by this trigger will be executed as part of same transaction whenever a new document will be inserted into collection A? If not, is there any other way to achieve this without triggering a multi document transaction from client code?
Hi @shailendra – Today all Triggers will be fired on operations after they have been fully committed to MongoDB so a Trigger’s response to an operation will always be out of scope of the original operation. I think the best way to accomplish this might be with a field that indicated whether a corresponding Trigger had acted on the document after the insertion (but as you point out a multi-documents transaction from the client should also be able to accomplish this).
The purpose of using such a trigger is to enforce referential integrity so that parent documents in collection B can’t be deleted when one or more children documents are present in collection A. I have a plan to use a field called childrenCount in collection B and then validate that the value of this field is 0 before deleting a document in collection B. However if trigger is not running in same transaction then I have no option other than using multi documents transaction at app level.
Does mongodb has any plan to support referential/data integrity in near future? If not, what are the possible ways to achieve it other than doing it at app level (where it can be too difficult to implement it if entity / object graph has complex relationships)?