2016-04-13 82 views
1

我是Meteor新手,想弄清楚如何最好地設計這個數據庫來存儲和發佈數據。我認爲這將是有意義的使用這些包:流星+ MongoDB:數據庫模式

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中的參數並查詢兩個集合並創建模板佈局?

回答

0

您應該有一個對應於課程/課程關係的字段,與傳統數據庫中的類似。

例如:

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

或:

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

使用MongoDB的推薦模式是有非規範化的數據庫。

這意味着什麼只是你沒有任何relational db模式。在SQL中,您有關係數據庫範例並獲取需要使用表執行Join的詳細數據。在mongodb中,不是在文檔(行)中放置引用,而是將整個對象放在這裏。所以實際上沒有join

所以在你的課的模式,你可以做這樣的事情,

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

course: { 
    type: Schemas.Course 
} 

如果你真的想在SQL way使用你的數據庫,有方便的程序包[publish composite][1]這將有助於你join (artificially)你的表/集。