简体   繁体   English

在MongoDB中建模多对多关系

[英]Modelling many-to-many relationships in MongoDB

I am having difficulty coming up with schemas for a school app. 我在为学校应用程序设计构架时遇到困难。

In particular, I am trying to model the relationship between the different kinds of users (eg instructors, teaching assistants, and students) with the courses and tutorials that they belong to. 特别是,我正在尝试使用他们所属的课程和教程来模拟不同类型的用户(例如,讲师,助教和学生)之间的关系。

Here are my requirements: 这是我的要求:

  1. each course will have one to many tutorials; 每门课程将有一对多的教程;
  2. each course will be taught by one to many instructors; 每门课程将由一对多的讲师讲授;
  3. each course will have one to many students; 每门课程将有一对多的学生;
  4. each tutorial will have one to many teaching assistants; 每个教程将有一对多的助教;
  5. each instructor will teach one to many courses; 每位讲师将教授一对多的课程;
  6. each teaching assistant may have one to many tutorials in one to many course; 每个助教在一对多的课程中可能有一对多的教程;
  7. each student will be enrolled in one to many courses; 每个学生将参加一对多的课程;
  8. each student may belong to one tutorial in the course that they are enrolled in; 每个学生在其所注册的课程中可能属于一个教程;

So far, the following are my schemas for the user, course, and tutorial collections. 到目前为止,以下是我针对用户,课程和教程集合的架构。

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
};

The problem lies with my requirements 5 to 8 -- which is more so the relationship from the User to the other models. 问题出在我的5到8的要求上-更重要的是从用户到其他模型的关系。 What could be a good way to model these relationships? 建模这些关系的好方法是什么?

One way, I thought of doing it eg req 5 was to add a field to the User schema 我想到的一种方法是,例如要求5是向User模式添加一个字段

    instructor: {
        courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
    }

But the problem happens when I do eg req 6. similarly because it will complicate the queries (eg "find all the tutorials in a course that the user is a teaching assistant in"). 但是,当我执行第6项要求时,也会发生问题,类似地,因为它会使查询复杂化(例如“在用户是助教的课程中查找所有教程”)。

    teachingAsst: {
        courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }]
        tutorials: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tutorial' }]
    }

In your case, Design is many to many relations. 就您而言,设计是多对多的关系。 So you have two approach for your problem. 因此,您有两种解决方法。

  1. Reference Document 参考文件
  2. Embedded Document 嵌入式文件

Embedded approach will have duplicate data which is difficult to update and delete where as the read operation will be much efficient due to single query. 嵌入式方法将具有难以更新和删除的重复数据,这是因为由于单个查询,读取操作将非常有效。

In case of the Reference Approach, your data will be demoralized. 如果是参考方法,您的数据将被贬低。 So, update and delete operation will be easy where as the read operation will have multiple hits on the database. 因此,更新和删除操作将很容易,因为读取操作将对数据库产生多次命中。

So, based on the your application requirement you should have to decide the appropriate approach. 因此,根据您的应用程序要求,您必须确定适当的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM