我有圖書頁面提交,我擁有圖書內容及其獨特信息,但一些數據(如authors
和areas
)保存在不同的館藏中,並通過圖書_id
鏈接到數據庫。我正在這樣做,但我幾乎肯定這不是最好的辦法。我應該如何將數據保存在流星的不同數據庫集合中?
Meteor.methods({
bookInsert: function(book, authors, areas) {
// save unique book information
var bookId = Books.insert(book);
// save each author on a single row on Authors collection
Object.keys(authors).forEach(function (singleAuthor) {
var thisAuthor = {
authorName: authors[singleAuthor],
bookId: bookId
}
var authorId = Authors.insert(thisAuthor);
});
//save each area on a single row on Areas collection
Object.keys(areas).forEach(function (singleArea) {
var thisArea = {
areaName: areas[singleArea],
bookId: bookId
}
var areaId = Areas.insert(thisArea);
});
return {
_id: bookId
}
}
})
的問題是,如果Areas
插入例如出現一定的誤差,本數據和作者的數據已經被保存在數據庫中,因爲它來自區域插入之前。
有沒有更好的方法將所有這些數據保存在不同的集合中,這樣可以防止這種問題?或者這是在不同集合上保存多個數據的正確方法?
謝謝,馬特。這確實是一個「SQL框架思維」的問題。我將研究更多關於NoSQL模式的知識。 – 2015-04-06 16:33:47