user1400803
user1400803

Reputation:

Meteor + MongoDB: Database pattern

I'm new to Meteor and trying to figure out how to best design this database to store and publish data. I thought it would make sense to use these packages:

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

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

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

I'd have a collection for the list of courses to be listed on a page:

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

And a collection for the lessons inside each course:

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

and have a admin page to create new courses/lessons using the autoform package.

My questions is how would I link the course to the lessons related to it? Would I use iron:router to listen to parameters in the url and query both collections and create the template layout?

Upvotes: 1

Views: 70

Answers (2)

Faysal Ahmed
Faysal Ahmed

Reputation: 1542

The recommended pattern used for mongodb is to have denormalization in your database.

What it means simply is that you don't have any relational db pattern. In SQL, you have relational database paradigm and to fetch a detailed data you need to do Join with tables. In mongodb, instead of putting a reference in your document(row), you put the whole object here. so there's no join actually.

so in your lesson schema you can do something like this,

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

course: {
    type: Schemas.Course
}

if you really want to use your database in SQL way , there's handy package [publish composite][1] which will help you join (artificially) your tables/collections.

Upvotes: 0

Hubert OG
Hubert OG

Reputation: 19544

You should have a field corresponding to the Lesson / Course relation, similarly as in traditional databases.

For example:

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

or:

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

Upvotes: 0

Related Questions