Search code examples
mongodbmorphiacascading-deletes

Delete all records dependent on a deleted reference in morphia?


I have a one to many relationship between foo and bar. I want to ensure that whenever a foo is deleted all bar that reference it will be deleted as well. Is there a way to ensure this will happen in morphia? I can write delete query to look for all dependent objects and remove them as well, but the problem is that the bar objects also have things referencing them that I want to be removed and I would like to ensure the delete logic cascades automatically.


Solution

  • You'll have to issue a separate delete call to remove the referenced documents. Mongo doesn't support relationships nor cascading deletes. You might consider embedding those documents rather than using references.