首页 > 解决方案 > 用于具有不同角色的用户和配置文件的 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,
})

但在这种情况下,我认为我需要创建一个配置文件字段并通过用户的更新请求来验证多个角色的不同表单信息。看起来只有一个请求的工作量太大(或者这是正常做法吗?)。为了验证,我考虑中间件。

或者我可以创建另一个方案FirstRoleProfileSecondRoleProfile通过引用连接它。对于这种结构,我可以创建不同的请求,例如:

那么为具有不同角色的用户和配置文件创建模式的最佳方法是什么?

标签: node.jsmongodbmongooseuser-profile

解决方案


推荐阅读