问题描述
我想创建一个具有多个用户角色和这些角色的多种类型的配置文件的项目。但我不知道哪种结构最适合这种逻辑。
我想像这样为用户嵌入子文档:
const FirstRoleProfile = new Schema({
// some profiles and unique fields for this roles
});
const SecondRoleProfile = new Schema({
// some profiles and unique fields for this roles
});
const User = new Schema({
// some users field like `name`,`email`,etc
role: {
type: String,enum: ['first role','second role'],required: true,},firstRole: FirstRoleProfile,secondRole: SecondRoleProfile,})
但在这种情况下,我认为我需要创建一个配置文件字段并通过用户的更新请求验证多个角色的不同表单信息。看起来只有一个请求的工作太多了(或者这是正常的做法吗?)。为了验证,我考虑了中间件。
或者我可以为 FirstRoleProfile
和 SecondRoleProfile
创建另一个 Scheme 并通过引用连接它。对于这个结构,我可以创建不同的请求,例如:
- 创建或更新 FirstRoleProfile(并验证此角色的唯一字段)
- 创建或更新 SecondRoleProfile(并验证此角色的唯一字段)
那么,为具有不同角色的用户和配置文件创建架构的最佳方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)