从我阅读文档和模型关系示例中,我没有看到如何在 MongoDB 中保存一对多关系。我有以下型号:
1. Category
2. Post
Category
有很多 Posts
并且 Post 属于 Category
我使用的外键是“categoryId
”
我使用以下脚本将示例数据插入 MongoDB:
create-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
});
});
};
create-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”只是临时的,我希望将实际的categoryId从MongoDB保存到每个帖子对象。
我的问题是:将每个帖子对象保存到 MongoDB 时,如何获取所有者的 ID(在本例中为categoryId)。我的MongoDB中保存的类别如下,每个类别都有一个ObjectId类型的_id
。 Post集合中是否需要将类别ObjectId保存为外键?如果是,怎么做?
{
"_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"
}
最佳答案
托尼给你一些建议,
- NoSQL 不相信标准化。在许多地方存储相同的数据是可以的。
- 在 MongoDB 中,如果一对多关系不太大,则可以在单个文档中处理一对多关系,即
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 的真正优势。
关于mongodb - 环回: Saving one-to-many relations in MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758144/