1

I am developing my first database in Mongo . I m using Mongoose to create the models . I want to implement a multiple relationship one to many. There are three models :User, Group and Role Model. where a user can belong to multiple groups and can have several roles in the same group. For example, John belongs to group 1 and 2. Juan in group 1 is administrator and the group 2 is administrator and superuser. Below I show the relational schema:

Schema relational

I have create the follow models:

UserModel

    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);

RoleModel

 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);

GroupModel

   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);

My questions are:

  1. This OK the implementations?
  2. When I want create an GroupUserRole document , how do it?

I have seen information about the method populate in mongoose (Populate) but only there is one relation ship between two models Thank for your help.

arisolarpower
  • 45
  • 1
  • 6

1 Answers1

2

Modelization

Ok, the question here is, do you want to create a "relational database"-like using mongodb or do you want to modelize a "NoSQL"-like database ?

I see what you are trying to do, reproducing the relational schemas. This is a common mistake and here is an explanation of why you should avoid this. Using mongodb, there is some new concepts you should be aware like a new relation (one to some, many to some) where some represent a group/list of a small number of document (we can call that sub-document). The main benefit of mongodb is to make less collection if it is possible.

If you want an explanation of how subdocument work in mongoose, here is the link http://mongoosejs.com/docs/subdocs.html

To solve your problem, I think (if you have not so many groups and roles that I suppose), you should do that :

UserModel

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);

And now you have one collection 'User' where you can manage your users, their groups and their roles.

Usage

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

Once you have your model, you can set groups and roles easily and then save it in the database.

Odonno
  • 409
  • 3
  • 5
  • First thank you for your reply. But I have the next questions: Imagine that Odonno and 1000 users more belong group 1 and group 2. In the group 1 , all this users have role of Admin and User, and group 2 only Admin role. If I want to update the roles in group 1, for example delete the user role, would have to go all users and check if they belong to the group 1 and if you have the user role. According to this post http://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference), I think I should use references rather than embed, as all my relationships depend on other. – arisolarpower Jul 28 '16 at 13:14
  • Clearly, it depend on your needs. If you have a dependency between Group and Role, it is way more interesting to create a collection for Group and use the objectId of group documents, like you said, references. You have a good example here : http://mongoosejs.com/docs/2.7.x/docs/populate.html – Odonno Jul 28 '16 at 13:20