Mongoose/ MongoDB 数据库设计
Mongoose/ MongoDB Database Design
我的模型 (GroupName) 和 1-x (Members) 的动态部分总是有一定的固定结构。
Group1
GroupName
Member 1
Member 2
Group2
GroupName
Member 1
Group3
GroupName
Member 1
Member 2
Member 3
是否最好使用两个表,然后通过这样的 id 连接它们:
Groups:
Group1
GroupName
GroupId
Group2
GroupName
GroupId
Members:
Member 1
GroupId
Member 2
GroupId
或使用 Schema.Types.Mixed
(或其他任何东西)?以及第二种方式如何实现?
以后我会一直组合使用它们。凭直觉我会选择第一种方法:
http://blog.mongolab.com/2013/04/thinking-about-arrays-in-mongodb/
编辑:
但即使在第二种方法上我也有问题,一个成员可以属于多个组,我不想将他存储两次。这些组是唯一的,只存在一次。
但我是 MongoDb 的新手,所以我想了解什么是最佳选择以及原因。
编辑二:
我选择了两个将它分成两个文档。这个模式的实现是否像这样正确:
var mongoose = require('mongoose');
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [id: Schema.Types.ObjectId, name: String]
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
&&
var mongoose = require('mongoose');
// define the schema for member model
var memberSchema = mongoose.Schema({
id: {
type:Schema.Types.ObjectId,
required: true,
unique: true
},
amount: String,
name: String
});
// create the model for users and expose it to our app
module.exports = mongoose.model('member', memberSchema);
MongoDB 博客上有一篇很棒的文章 post,它告诉我们可以根据模型关系设计模式的各种方法。
我认为最适合您的模式是使用带有成员 ID 的嵌入式数组。
//Group
{
_id: '1234',
name: 'some group',
members : [
'abcd',
'efgh'
]
}
编辑
架构中需要更正:
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [{id: Schema.Types.ObjectId, name: String}] //Needs to be enclosed with braces
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
我不知道您的文档包含什么以及成员是否是一个不断增长的数组 - 例如 Group1
在任何给定时刻都可以有 1-n members
。如果是这种情况,您应该选择选项 2:尝试类似的方法:
{gId: 1, mId: 5}
这是最适合 Social graph
的设计。你的 Group documents
将有一个固定的大小,这有利于记忆,你可以很容易地得到一个组的所有成员(只是不要忘记索引 gId
和 mId
)
如果每个组的成员数量都是固定的(或者增减幅度不大),那么选择选项 1
mongoDb 团队(以及 src 代码)有一个很棒的 post 讨论设计。
我的模型 (GroupName) 和 1-x (Members) 的动态部分总是有一定的固定结构。
Group1
GroupName
Member 1
Member 2
Group2
GroupName
Member 1
Group3
GroupName
Member 1
Member 2
Member 3
是否最好使用两个表,然后通过这样的 id 连接它们:
Groups:
Group1
GroupName
GroupId
Group2
GroupName
GroupId
Members:
Member 1
GroupId
Member 2
GroupId
或使用 Schema.Types.Mixed
(或其他任何东西)?以及第二种方式如何实现?
以后我会一直组合使用它们。凭直觉我会选择第一种方法: http://blog.mongolab.com/2013/04/thinking-about-arrays-in-mongodb/
编辑: 但即使在第二种方法上我也有问题,一个成员可以属于多个组,我不想将他存储两次。这些组是唯一的,只存在一次。
但我是 MongoDb 的新手,所以我想了解什么是最佳选择以及原因。
编辑二: 我选择了两个将它分成两个文档。这个模式的实现是否像这样正确:
var mongoose = require('mongoose');
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [id: Schema.Types.ObjectId, name: String]
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
&&
var mongoose = require('mongoose');
// define the schema for member model
var memberSchema = mongoose.Schema({
id: {
type:Schema.Types.ObjectId,
required: true,
unique: true
},
amount: String,
name: String
});
// create the model for users and expose it to our app
module.exports = mongoose.model('member', memberSchema);
MongoDB 博客上有一篇很棒的文章 post,它告诉我们可以根据模型关系设计模式的各种方法。
我认为最适合您的模式是使用带有成员 ID 的嵌入式数组。
//Group
{
_id: '1234',
name: 'some group',
members : [
'abcd',
'efgh'
]
}
编辑
架构中需要更正:
// define the schema for group model
var groupSchema = mongoose.Schema({
href: {
type: String,
required: true,
unique: true
},
title: String,
members: [{id: Schema.Types.ObjectId, name: String}] //Needs to be enclosed with braces
});
// create the model for users and expose it to our app
module.exports = mongoose.model('group', groupSchema);
我不知道您的文档包含什么以及成员是否是一个不断增长的数组 - 例如 Group1
在任何给定时刻都可以有 1-n members
。如果是这种情况,您应该选择选项 2:尝试类似的方法:
{gId: 1, mId: 5}
这是最适合 Social graph
的设计。你的 Group documents
将有一个固定的大小,这有利于记忆,你可以很容易地得到一个组的所有成员(只是不要忘记索引 gId
和 mId
)
如果每个组的成员数量都是固定的(或者增减幅度不大),那么选择选项 1
mongoDb 团队(以及 src 代码)有一个很棒的 post 讨论设计。