用户到用户数据库对象结构

User to User Database Object Structure

在教师对学生应用程序中构建数据库中的对象的最佳做法是什么?更具体地说,一个老师将有多个学生,而学生可能有多个与他们互动的老师。现在,每个 class 老师正在指导的人都有一个 "class id",当学生注册时,他们会将相同的 class ID 添加到他们模型中的列表中。当老师想查看花名册时,我使用

Student.find({"class-id": "abc123"}, function(err, student){
    if(err){
       console.log(err)
    } else {
       res.render("/roster", {student: student});
    }
 });

"abc123" 是一个示例 class 代码。

目前这是非常基本的代码 -- 只是开始一个粗略的应用程序。我是否应该在教师和学生之间创建某种类型的具有某种层次结构的不同数据库结构?

对于这个基本且可能令人困惑的问题,我深表歉意,我是网络开发的新手,只是不想把自己逼到墙角。

我正在使用 MEAN 堆栈,但我认为这是一个更概念化的问题。

谢谢

dmfay 的评论很有见地。对于关系模型,使用猫鼬是次优的。但是,您当然仍然可以使用它。它会是这样的(顺便说一句,你不清楚这一点,但它暗示每个学生和老师都有多个 class-ids,我在那个假设下工作)

var mongoose = require("mongoose");

var StudentSchema = new mongoose.Schema({
    //other student details
    teachers: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Teacher"
    }],
    classIds: [STRING]
});

module.exports = mongoose.model("Student", StudentSchema);

然后教师模式将遵循相同的路线:

var mongoose = require("mongoose");

var TeacherSchema = new mongoose.Schema({
    //other teacher details
    students: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Student"
    }],
    classIds: [STRING]
});

module.exports = mongoose.model("Teacher", TeacherSchema);

那么,你的做法是,当一个学生注册 class 时,你会在他们的 classIds 数组中找到所有具有该 classId 的教师,你循环在他们之上将学生添加到他们的每个学生数组中,然后将他们(教师)添加到学生的教师数组中。

当你想搜索 class 中的所有学生时,你可以像以前那样做,除了 classIds 是一个字符串数组,而不是单个字符串,你需要检查它们是否匹配