0

I want to conceive a relationship such as that if i destroy a parent object the child object is removed too on MongoDB (Spring) How to achieve this ? I know that in Python's Flask this could be done with EmbeddedDocumentField but how to do it in Java's Spring boot.

Thank you,

  • MongoDB doesn't support cascading deletes. Please check this [post](https://stackoverflow.com/questions/52148305/how-to-cascade-delete-document-in-mongodb/52269205) – Swapnil Khante Jul 10 '20 at 12:38

2 Answers2

0

MongoDB doesn't support cascading deletes. You should probably create an array in the User object, and put the complete child documents into that array instead of keeping them in their own collection. That way they will be deleted together with the parent, because they are a part of it.

checkout this post

Swapnil Khante
  • 547
  • 2
  • 10
  • I don't want to put the complete child documents into the parent object (through an array) because that's would affect the way i create the parent (i would have to set fields of the child object too when i create the parent which i don't want) – user1319236 Jul 10 '20 at 13:11
  • This is more like a limitation of nosql dbs – Swapnil Khante Jul 10 '20 at 13:14
0

There are two ways I can think of

  • If parent deletion is in your control , you can use the transaction delete the child also .
  • If parent deletion is not in your control, you can listen to change stream and then when deletion happens, delete the child.