node.js - 用于具有不同角色的用户和配置文件的 MongoDB 方案
问题描述
我想创建一个具有多个用户角色和这些角色的多种类型的配置文件的项目。但我不知道哪种结构最适合这种逻辑。
我想像这样为用户嵌入子文档:
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
通过引用连接它。对于这种结构,我可以创建不同的请求,例如:
- 创建或更新 FirstRoleProfile(并验证此角色的唯一字段)
- 创建或更新 SecondRoleProfile(并验证此角色的唯一字段)
那么为具有不同角色的用户和配置文件创建模式的最佳方法是什么?
解决方案
推荐阅读
- c# - Paket Newtonsoft ASP.NET 4.5
- prolog - 为什么 prolog 中的 length/2 谓词可以产生无限数量的长度?
- dc.js - 谁还需要维度?
- java - JxBrowser 网页到 java swing 面板拖放
- docker - 从 golang 主机应用程序连接到在 Docker 中运行的 elasticsearch 6.4
- ruby-on-rails - Rails 或 sinatra 专用于应用程序的内核
- python - 根据Python中文件名的数值对文件进行排序
- c# - 计算年利率
- django - 将初始(和正在进行的)数据加载到 Django 中的标准方法
- swift - 如何在具有一对多关系的核心数据实体中附加记录