title: What is the recommended equivalent of cascaded delete in MongoDB for N:M relationships?
body: <p>Assuming the following "schema/relationship" design what is the recommended practice for handling deletion with cascade delete like operation?</p>
  
  <p>Relational Schema:</p>
  
  <pre>
    +---------+                                    +--------+
    | Student |-*--------1-[Enrollment]-1--------*-| Course |
    +---------+                                    +--------+
  </pre>
  
  <p>MongoDB:</p>
  
  <pre>
    +---------+                    +--------+
    | Student |-*----------------*-| Course |
    +---------+                    +--------+
  </pre>
  
  <p>Given this classic design of enrollment of students to courses, having a collection of courses in students and vice versa seems to be an appropriate data model when using MongoDB (that is nothing for the relationship/enrollment table). But coming from a relational world how should I handle the semantics of deleting a course? That is, when a course is deleted, all the "enrollment" records should be deleted too. That is, I should delete the course from the collection of each student record. It looks like I have to fire 2 queries: one for deleting the course and then to delete it from each student's collection. Is there a way to have a single query to perform this "cascade delete" like semantic without the additional query? Does the data model need to change?</p>
  
  <p>NOTE: For all other use cases the above data model works just fine:</p>
  
  <ul>
  <li>Deleting a student <code>=&gt;</code> just delete that student and associated collection of courses deleted along with it.</li>
  <li>Student willing to drop a course <code>=&gt;</code> just delete it from the student collection of courses</li>
  <li>Add student/course <code>=&gt;</code> just add it to corresponding 'table' in essence.</li>
  </ul>
  
  <p>The only tricky thing is handling the deletion of a course. How should I handle this scenario in MongoDB, since I hail from a relational background and am unable to figure this one out.</p>