MongoDb Handle Multiple Document Insert?

I am new to Mongodb lets say i need to insert 3 documents in 3 different collections for example lets say i have a department, students and department_membership collections. when department head add one student to the department, so that time i need insert document like

{ _id: 123, name: "st_name", email: "test@gmail.com" }

in student collection

{ _id: 2356, dept_id: 12 , added_by_whom_id: 253, student_id: 123}

in department collection

{ _id: 13111, dept_id: 12, student: { _id: 123, name: "st_name" } }

in department_membership collection

what i want is to either insert all documents in respective collections or it should fail all insert, how to handle this?

This is not possible with MongoDB without rolling out some custom code to handle transactions.

This is one of the primary limitations of MongoDB - lack of transactions.

1 Like

when i googled i came to know about two phase commits, is it possible to do with two phase commits?

1 Like

That’s exactly what I meant by “without rolling out some custom code to handle transactions”.

2 Likes