json - MongoDB如何使用猫鼬模式将角色连接到用户
问题描述
我想构建一个应用程序,用户可以注册为“私人”或“公司”,如果用户注册为公司,他们将有更多选择,例如“包 1”、“包 2”、“包 3”等。这些附加选项是不同的“角色”。
我正在考虑这里的最佳做法。我正在使用MongoDB
,所以我应该创建一个包含所有内容的“UserSchema”还是应该创建某种“RoleSchema”?
到目前为止,我有这样的事情:
用户.js:
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
roleType: {
type: Schema.Types.ObjectId,
ref: "roles"
}
});
module.exports = User = mongoose.model("users", UserSchema);
角色.js
const RoleSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users"
},
role_name: {
type: String
}
});
module.exports = Roles = mongoose.model("roles", RoleSchema);
这是一个好方法还是可能有其他更好的方法来做到这一点?我对 MongoDB 有点陌生
提前致谢
解决方案
推荐阅读
- windows - 从命令行设置 Windows 10 HDR 开/关或 HDR/SDR 平衡?
- r - 创建具有四个角坐标的矩形多边形 shapefile
- r - R,dplyr:当每个组有多行而不创建新的数据框时,有没有办法添加组的顺序?
- android - 注册页面上的登录按钮直接进入 MainActivity 而无需实际登录
- c# - C# 仅在没有美分时使用 String.Format 删除货币中的小数
- c# - 带静态类的反序列化对象
- javascript - 使用 Amazon CDK 创建事件桥堆栈
- java - 发布请求不适用于自签名 SSL
- python - Scrapy:无法绑定:24:打开的文件太多
- java - 如何在不再次读取相同元素的情况下以随机顺序获取数组元素?