【问题标题】:Meteor + MongoDB: Database patternMeteor + MongoDB:数据库模式
【发布时间】:2016-04-13 19:29:26
【问题描述】:

我是 Meteor 的新手,正在尝试找出如何最好地设计这个数据库来存储和发布数据。我认为使用这些包是有意义的:

https://github.com/aldeed/meteor-autoform

https://github.com/aldeed/meteor-simple-schema

https://github.com/iron-meteor/iron-router

我有一个要在页面上列出的课程列表的集合:

Courses = new Mongo.Collection("courses");

Courses.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  comingSoon: {
    type: boolean,
    label: "Coming Soon?"
  },
  description: {
    type: String,
    label: "Course Description",
    max: 200
  }
}));

以及每门课程中的课程合集:

Lessons = new Mongo.Collection("lessons");
Lessons.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  video: {
    type: String,
    label: "Link to video"
  },
}));

并有一个管理页面来使用 autoform 包创建新课程/课程。

我的问题是如何将课程链接到与之相关的课程?我会使用 iron:router 来监听 url 中的参数并查询两个集合并创建模板布局吗?

【问题讨论】:

    标签: mongodb meteor iron-router simple-schema


    【解决方案1】:

    您应该有一个对应于课程/课程关系的字段,类似于在传统数据库中。

    例如:

    Lessons.attachSchema(new SimpleSchema({
      ...
      courseId: {type: String}, // ID of the corresponding course
    }));
    

    或:

    Courses.attachSchema(new SimpleSchema({
      ...
      lessonIds: {type: [String]}, // Array of IDs of lessons
    }));
    

    【讨论】:

      【解决方案2】:

      用于 mongodb 的推荐模式是在您的数据库中进行非规范化。

      这意味着您没有任何relational db 模式。在 SQL 中,您有关系数据库范例,并且要获取详细的数据,您需要使用表执行 Join。在 mongodb 中,不是在文档(行)中放置引用,而是将整个对象放在这里。所以实际上没有join

      所以在你的课程架构中你可以做这样的事情,

      Schemas.Course = new SimpleSchema(...);

      course: {
          type: Schemas.Course
      }
      

      如果你真的想在 SQL way 中使用你的数据库,有一个方便的包 [publish composite][1] 它将帮助你 join (artificially) 你的表/集合。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-06
        • 2021-01-06
        • 2017-04-17
        • 2021-02-01
        • 2013-10-10
        相关资源
        最近更新 更多