【发布时间】:2017-01-02 00:35:57
【问题描述】:
我很难为学校应用提出架构。
特别是,我试图模拟不同类型的用户(例如教师、助教和学生)与他们所属的课程和教程之间的关系。
这是我的要求:
- 每门课程都有一对多的教程;
- 每门课程将由一对多讲师授课;
- 每门课程都有一对多的学生;
- 每个教程都有一对多的助教;
- 每位讲师将教授一对多课程;
- 每个助教可能有一对多课程中的一对多教程;
- 每个学生将参加一对多课程;
- 每个学生可能属于他们所注册课程的一个教程;
到目前为止,以下是我的用户、课程和教程集合的架构。
var CourseSchema = new mongoose.Schema({
name: { type: String, required: true },
code: { type: String, required: true },
tutorials: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tutorial' }], // 1
instructors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }], // 2
students: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }] // 3
});
var TutorialSchema = new mongoose.Schema({
number: { type: String, required: true },
teachingAsst: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }] // 4
});
var UserSchema = new mongoose.Schema({
email: { type: String, lowercase: true },
password: String,
name: {
first: { type: String, lowercase: true },
last: { type: String, lowercase: true }
},
roles: [String] // instrutor, teachingAsst, student
};
问题出在我的要求 5 到 8 上——用户与其他模型的关系更是如此。建模这些关系的好方法是什么?
一种方式,我想过这样做,例如req 5 是向用户模式添加一个字段
instructor: {
courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
}
但是当我这样做时,问题就会发生。 req 6. 同样,因为它会使查询复杂化(例如“查找用户是助教的课程中的所有教程”)。
teachingAsst: {
courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
tutorials: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tutorial' }]
}
【问题讨论】:
标签: mongodb mongoose many-to-many