从我阅读的文档和模型关系示例中,我没有看到如何在MongoDB中保存一对多关系。我有以下型号:
1. Category
2. Post
Category
有很多Posts
,而Post属于Category
我使用的外键是" categoryId
"
我使用以下脚本将示例数据插入MongoDB:
创建-categories.js:
var categories = [
{"title" : "Technology Matters", "description": "Blogs on latest technologies"},
{"title" : "Innovative Ideas", "description": "Innovative ideas on the next project"},
{"title" : "Comments on Hot Topics", "description": "My comments on the hot topics"}
];
module.exports = function(server) {
var dataSource = server.dataSources.mongoDatastore;
dataSource.automigrate('Category', function(err) {
if (err) throw err;
var Model = server.models.Category;
//create some sample data
var count = categories.length;
categories.forEach(function(category) {
Model.create(category, function(er, result) {
if (er) return;
console.log('Category created: ', result);
count--;
if (count == 0) {
console.log('Categories all created!');
dataSource.disconnect();
}
});
//could define a model scope here
});
});
};
创建-posts.js:
var posts = [
{"title" : "Post 1 Title", "bodyText": "body text for blog post 1", "dateCreated": new Date(), "categoryId": 1},
{"title" : "Post 2 Title", "bodyText": "body text for blog post 2", "dateCreated": new Date(), "categoryId": 1},
{"title" : "Post 3 Title", "bodyText": "body text for blog post 3", "dateCreated": new Date(), "categoryId": 2},
{"title" : "Post 4 Title", "bodyText": "body text for blog post 4", "dateCreated": new Date(), "categoryId": 2},
{"title" : "Post 5 Title", "bodyText": "body text for blog post 5", "dateCreated": new Date(), "categoryId": 1},
{"title" : "Post 6 Title", "bodyText": "body text for blog post 6", "dateCreated": new Date(), "categoryId": 3},
{"title" : "Post 7 Title", "bodyText": "body text for blog post 7", "dateCreated": new Date(), "categoryId": 2},
{"title" : "Post 8 Title", "bodyText": "body text for blog post 8", "dateCreated": new Date(), "categoryId": 3},
{"title" : "Post 9 Title", "bodyText": "body text for blog post 9", "dateCreated": new Date(), "categoryId": 1},
{"title" : "Post 10 Title", "bodyText": "body text for blog post 10", "dateCreated": new Date(), "categoryId": 2}
];
module.exports = function(server) {
var dataSource = server.dataSources.mongoDatastore;
dataSource.automigrate('Post', function(err) {
if (err) throw err;
var Model = server.models.Post;
//create some sample data
var count = posts.length;
posts.forEach(function(post) {
Model.create(post, function(er, result) {
if (er) return;
console.log('Post created: ', result);
count--;
if (count == 0) {
console.log('Posts all created!');
dataSource.disconnect();
}
});
//could define a model scope here
});
});
};
请注意那个' categoryId'在上面的每个帖子对象中伪造只是暂时的,我希望将MongoDB中的实际categoryId保存到每个帖子对象。
我的问题是:在将每个帖子对象保存到MongoDB时,您将如何获得所有者的ID(在本例中为categoryId)。我的MongoDB中保存的类别如下,每个类别都有_id
,即ObjectId类型。是否有必要将类别ObjectId保存为Post集合中的外键?如果是,怎么样?
{
"_id" : ObjectId("55355f076ed9d911089ea0a7"),
"title" : "Technology Matters",
"description" : "Blogs on latest technologies"
}
{
"_id" : ObjectId("55355f076ed9d911089ea0a8"),
"title" : "Innovative Ideas",
"description" : "Innovative ideas on the next project"
}
{
"_id" : ObjectId("55355f076ed9d911089ea0a9"),
"title" : "Comments on Hot Topics",
"description" : "My comments on the hot topics"
}
答案 0 :(得分:2)
Tony给你的建议很少,
one < many
而不是one << many
和cardinality of one is not very high
,则可以在单个文档中处理一对多关系。您的问题的解决方案可能是,一个类别可以有多个帖子,但总共会有多少个类别。在我看来,与帖子相比,一个(类别)的数量非常少。
存储数据的更好方法可以是,
创建一个名为posts的集合,
{
"title" : "Post 1 Title",
"bodyText": "body text for blog post 1",
"dateCreated": new Date(),
"category": "movie"
}
在某些情况下,帖子可能会属于2类
{
"title" : "Post 1 Title",
"bodyText": "body text for blog post 1",
"dateCreated": new Date(),
"category": ["movie","drama"]
}
这仍然是一个很好的设计,并且可以很好地处理大量数据。由于所有相关数据都存储在NoSQL的真正优势位置,因此操作性能也会提高。