【问题标题】:How to improve this data model, schema and relations如何改进此数据模型、模式和关系
【发布时间】:2015-10-16 13:09:30
【问题描述】:

我使用了流星、mongoDB 和自动表单。我的数据模型中有很多实体。 学院有一些学生和教师和班级。班级有一些学生和一名老师,每个学生都有一些成绩。

我设计了这个架构:

Schema = {};
Schema.UserProfile = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    },
    family: {
        type: String,
        label: "Family"
    },
    address: {
        type: String,
        label: "Address",
        optional: true,
        max: 1000
    },
    workAddress: {
        type: String,
        label: "WorkAddress",
        optional: true,
        max: 1000
    },
    phoneNumber: {
        type: Number,
        label: "Phone Number",
        optional: true
    },
    mobileNumber: {
        type: Number,
        label: "Phone Number"
    },
    birthday: {
        type: Date,
        optional: true
    },
    gender: {
        type: String,
        allowedValues: ['Male', 'Female'],
        optional: true
    },
    description: {
        type: String,
        label: "Description",
        optional: true,
        max: 1000
    }

});
Schema.User = new SimpleSchema({
    username: {
        type: String,
        regEx: /^[a-z0-9A-Z_]{3,15}$/
    },
    emails: {
        type: [Object],
        // this must be optional if you also use other login services like facebook,
        // but if you use only accounts-password, then it can be required
        optional: true
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date,
        optional: true
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
//    // Add `roles` to your schema if you use the meteor-roles package.
//    // Option 1: Object type
//    // If you specify that type as Object, you must also specify the
//    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
//    // Example:
//    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
//    // You can't mix and match adding with and without a group since
//    // you will fail validation in some cases.
//    roles: {
//        type: Object,
//        optional: true,
//        blackbox: true
//    }
//    // Option 2: [String] type
//    // If you are sure you will never need to use role groups, then
//    // you can specify [String] as the type
    roles: {
        type: [String],
        optional: true
    }
});
//Meteor.users.attachSchema(Schema.User);

Schema.Grade: {
    student: Schema.User,
class: Schema.Class,
    gradeValue: value
}

Schema.Class: {
    teacher: Schema.User,
    students: {
        type: [Schema.User]
    },
    grades: {
        type: [Schema.Grade]
    }
}

Schema.Academy: {
    name: {
        label: "academy name",
        type: String
    },
    students: {
        type: [Schema.User]
    },
    teachers: {
        type: [Schema.User]
    },
    classes: {
        type: [Schema.Class]
    }
}

但我认为这不是最好的,也遇到过很多重复。

我需要以下查询:

academy classes
academy students
academy teachers
students classes
students grades
student profile
class students
class teacher
class grades
teacher students
teacher classes
teacher profile

【问题讨论】:

    标签: mongodb meteor meteor-accounts meteor-autoform meteor-helper


    【解决方案1】:

    首先是识别实体:student, teacher, class, academy

    其次,从最大的开始识别关系:

    • 1个学院有很多班(1:M)
    • 1 个班级有很多学生,1 个学生有很多班级 (M:N)
    • 1 位老师有很多课 (1:M)

    接下来,关系模式。这里的技巧是尽量减少数组的使用,因此对于每个 1:M 关系,将外键放在 M 上:

    • 学院:_id, name
    • 类:_id, name, teacherId, academyId
    • 老师:“简介”
    • 学生:“个人资料”

    所有外键(teacherId、academyId)的索引。

    现在我们没有任何重复,但我们仍然要处理学生:班级关系。

    Grades 集合的示例是教科书 3NF,SQL 方式。没有错,这是一个很好的解决方案。 (只要确保在外键上建立索引!)。

    或者,在学生文档上,您可以有一个对象,每个字段都是一个 classId,每个值都是成绩。

    另一种选择是在班级文档中添加一个学生对象,每个字段都是 studentId,每个值都是成绩。

    如何决定哪一个最好是访问模式。如果您有一个学生门户网站,他们可以在其中查看他们的课程和成绩,那么将其放在学生集合中更有意义,并且偶尔查询以获得班级平均值会更昂贵,因为它必须遍历所有学生.嵌套在类集合中的情况正好相反。

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2022-01-11
      相关资源
      最近更新 更多