Home > other >  How to delete multi level referenced schemas in Mongo-DB
How to delete multi level referenced schemas in Mongo-DB

Time:11-14

I am creating a ERP system in which School have courses and Courses contain Students.I want to impelement API to delete a school which also deletes courses and all the students enrolled in courses .

This is what i am doing in API logic:-

app.delete("/fetchSchool/:schoolId", async (req, res) => {
try {
    const deletedSchool = await schools.findByIdAndDelete(req.params.schoolId);
    (!deletedSchool) ? res.send('Invalid Request'):
    courses.remove({_id: {$in: deletedSchool.course}}, (error, deletedCourse) => {
        error ? res.send('Subsequent Deletion Of Course Failed'):
        students.remove({_id: {$in: deletedCourse.students}}, (error, response) => {
            error ? res.send(error): res.send('Deletion Successfull');
        })
    })

 } catch (error) {
     res.status(500).send('some error occured,'   error.message);
 }
})

But this only deletes school and courses in school but students data still present.

This is my schema:-

School Schema

 const SchoolSchema = mongoose.Schema({
 course: [{
     type: mongoose.Schema.Types.ObjectId,
     ref: 'course'
  }] 
 })

Course Schema

const CourseSchema = mongoose.Schema({
students: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'student'
 }]
})

Student Schema

const StudentSchema = mongoose.Schema({
name: {
    type: String,
    required: true
},
email: {
    type: String,
    unique: true
}})

Please let me know the corect way to delete students also along with school and courses.

CodePudding user response:

First, I would try and find the school. That is the last thing I would want to delete since we need the ID and information from it.

const school = await schools.findOne({ _id: req.parmas.schoolId })

So now that I have the information from the school, I'll move on and try to delete the Courses. Since the students are stored in an array, I'd leave those alone and only focus on removing the course.

Course.remove({ 'students.type': school._id }).

After removing the course(s), you should then delete the school.

school.remove()

CodePudding user response:

  • I think this will work:
  1. find the school.
  2. find the courses with school.courses.
  3. map through courses.
  4. deleteMany students with course.students.
  5. delete courses.
  6. delete school.
  • You can use Pre and Post hooks in Mongoose, but make sure to write it before mongoose.model() line in the model file.

  • There is more performant solution but I can't think about another one for now.

    app.get("/fetchSchool/:schoolId", async (req, res) => {
      const schoolId = req.params.schoolId;
    
      try {
          // find one school
          const findSchool = await schools.findOne({ _id: schoolId });
    
          if (findSchool) {
              // find the courses in the school.courses
              const findCourses = await courses.find({ _id: { $in: findSchool.courses } });
    
              if (findCourses) {
                  // map through courses to get every course
                  findCourses.map(async (course) => {
                      // find the students in the mapped course AND remove them
                      const deleteStudents = await students.deleteMany({ _id: { $in: course.students } })
    
                      if (deleteStudents) {
                          // Remove the courses
                          const deletedCourses = await courses.deleteMany({ _id: { $in: findSchool.courses } });
    
                          if (deletedCourses) {
                              // Remove the school
                              const deletedSchool = await schools.deleteOne({ _id: schoolId });
    
                              if (deletedSchool) {
                                  res.status(200).send('Deleted (school, courses, students) successfully');
                              } else { res.send("school not deleted") }
    
                          } else { res.send("courses not deleted") }
    
                      } else { res.send("courses not deleted") }
                  })
    
              } else { res.send("no courses") }
    
          } else { res.send("no school") }
    
      } catch (error) {
          res.status(500).send('some error occured, '   error.message);
      }
    })
    
  • Related