【问题标题】:How to delete the content of a list not only the list? If x is in y, delete y as well when x is deleted如何删除列表的内容不只是列表?如果 x 在 y 中,则在删除 x 时也删除 y
【发布时间】:2019-04-24 10:46:28
【问题描述】:

我有两个收藏,

  • 教室
  • 学生

这是他们的架构

**CLASSROOM SCHEMA**

const mongoose = require('mongoose');


const classroomSchema = new mongoose.Schema({
  classroomname:  {
    type: String
  },
  createdAt: { type: Date, default: Date.now },
  author: {
    id: {
        type: mongoose.Schema.Types.ObjectId, 
        ref: "User",

    },
   },
  students: {
    id : {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Student"
    }
  }

});

const Classroom = mongoose.model('Classroom', classroomSchema);

module.exports = Classroom;

和..

**STUDENT SCHEMA**

const mongoose = require('mongoose');


const studentSchema = new mongoose.Schema({
    fullName: {
        type: String
    },
    author: {
    id: {
        type: mongoose.Schema.Types.ObjectId, 
        ref: "User",

    },
   }, 
    classroom: {
                name: {
                        type: String
                        },
                id: {
                        type: mongoose.Schema.Types.ObjectId, 
                        ref: "classroom",
                    }
                },

});

const Student = mongoose.model('Student', studentSchema);

module.exports = Student;

这就是学生的样子

  • 其中 author.id 是 user.id,谁在登录后创建了学生 在。

我确实有以下代码可以成功删除学生

router.get('/delete/:id',passportConfig.isAuthenticated,(req, res) => {


    Student.findByIdAndRemove(req.params.id, (err, docs) => {
            if (!err) {
                res.redirect('/');
            }
            else {console.log('Error in classroom deletion:' +err);}

        });

    });

..以及用于删除教室的非常相似的代码。 我的问题是, 当我删除一个教室时,如何同时删除属于该教室的所有学生?

这是我的逻辑

find classroom id
find student.classroom.id
if student.classroom.id == classroom.id
Classroom, Student findbyid and remove

谢谢!

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    下面的代码会有所帮助:

    req.params.id 应该是教室 id。

    router.get('/delete/:id',passportConfig.isAuthenticated,(req, res) => {
      // To Delete Classroom by id 
      Classroom.remove({_id : mongoose.Schema.Types.ObjectId(req.params.id)}, (errClass, classroomRes) => { 
        if (!errClass) {
        console.log('Classroom Removed :',classroomRes);
        // delete stud which has classroom Id.
          Student.remove({'classroom.id' : mongoose.Schema.Types.ObjectId(req.params.id)}, (errStud, studentRes) => { 
            if(!errStud){
                console.log('studentRes Removed :',studentRes);
            }else{
                console.log('Error in Student deletion:',errStud);
            }
            return res.redirect('/');
          });
    
        }else{
           console.log('Error in Classroom deletion:',errClass);
           return res.redirect('/');
        }
      })
    });
    

    【讨论】:

    • 就像一个魅力,唯一需要修改的是:“mongoose.Types.ObjectId”而不是 mongoose.Schema.Types.ObjectId。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2021-02-14
    • 2016-09-25
    • 2023-02-16
    相关资源
    最近更新 更多