-1

If I'm not keeping a list of child_ids because I've been told that's redundant, and all the children have a reference to the parent document inside their schema, how would I go about it when I delete the parent document?

Before deleting I'd have to query all the children document with that parent id inside of it, correct? And then delete them all?

joe
  • 1,563
  • 4
  • 16
  • 35
  • *"Before deleting I'd have to query all the children document with that parent id inside of it, correct? And then delete them all?"* yup. mongodb doesn't really have a notion of foriegn keys and constraints, so either way that's what is going to happen. I'm not sure though whether or not mongoose has some kind of... poly fill for that. – Kevin B Jun 29 '16 at 15:28
  • Related (but probably out of date): http://stackoverflow.com/questions/14348516/cascade-style-delete-in-mongoose – Kevin B Jun 29 '16 at 15:35

1 Answers1

0

You can do something like this:

  Parent.remove({_id: <someID>}, function(err){
       Children.remove({_parent: <parentID>}, function(err) {
       })
   })

If you need some rollback on error when deleting parent\or children you should add it.

Dima Grossman
  • 2,800
  • 2
  • 21
  • 26