gpt4 book ai didi

node.js - Nodejs : Multiple One to many in MongoDB

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:35 25 4
gpt4 key购买 nike

我正在 Mongo 中开发我的第一个数据库。我正在使用 Mongoose 创建模型。我想实现一对多的多重关系。共有三种模型:用户、组和角色模型。其中一个用户可以属于多个组,并且可以在同一组中拥有多个角色。例如,John 属于组 1 和组 2。组 1 中的 Juan 是管理员,组 2 中的管理员和 super 用户。下面我展示了关系模式:

Schema relational

我创建了以下模型:

用户模型

    const userSchema = new Schema({
username: {type: String, unique: true},
first_name: String,
middle_name: String,
first_surname: String,
second_surname: String,
email: String,
password: String
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});
const UserModel = mongoose.model('user', userSchema);

榜样

 const roleSchema = new Schema({
name: {type: String, unique: true},
code: {type: String, unique: true},
description: String
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});
const RoleModel=mongoose.model('role', roleSchema);

组模型

   const groupSchema = new Schema({
name: {type: String, unique: true}
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});
const GroupSchema = mongoose.model('group', groupSchema);

GroupUserRoleModel

const groupuserroleSchema = new Schema({
role: {type: Schema.Types.ObjectID, ref: 'role'},
user: {type: Schema.Types.ObjectID, ref: 'user'},
group: {type: Schema.Types.ObjectID, ref: 'group'}
});
const GroupUserRoleModel = mongoose.model('group_user_role', groupuserroleSchema);

我的问题是:

  1. 这样可以实现吗?
  2. 当我想创建一个 GroupUserRole 文档时,该怎么做?

我已经看到有关 mongoose ( Populate ) 中填充方法的信息,但两个模型之间只有一种关系感谢您的帮助。

最佳答案

建模

好吧,这里的问题是,你想使用 mongodb 创建一个类似“关系数据库”的数据库,还是想建模一个类似“NoSQL”的数据库?

我明白你正在尝试做什么,重现关系模式。这是一个常见的错误,这里解释了为什么应该避免这种情况。使用 mongodb,您应该了解一些新概念,例如新关系(一对一,多对一些),其中 some 表示少量文档的组/列表(我们可以称之为子文件)。 mongodb 的主要好处是尽可能减少收集。

如果您想了解子文档如何在 mongoose 中工作,请点击链接 http://mongoosejs.com/docs/subdocs.html

为了解决您的问题,我认为(如果您没有我认为的那么多组和角色),您应该这样做:

用户模型

const roleSchema = new Schema({
name: {type: String, unique: true},
code: {type: String, unique: true},
description: String
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});

const groupSchema = new Schema({
name: {type: String, unique: true}
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});

const userSchema = new Schema({
username: {type: String, unique: true},
first_name: String,
middle_name: String,
first_surname: String,
second_surname: String,
email: String,
password: String,
roles: [roleSchema],
groups: [groupSchema]
}, {
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at', deleteAt: 'delete_at'}
});
const UserModel = mongoose.model('user', userSchema);

现在您有了一个“用户”集合,您可以在其中管理您的用户、他们的组及其角色。

用法

var user = new UserModel({ name: "Odonno", groups: [{ name: 'Admin' }, { name: 'User' }] })
user.save(callback);

获得模型后,您可以轻松设置组和角色,然后将其保存在数据库中。

关于node.js - Nodejs : Multiple One to many in MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com