title: Nodejs : Multiple One to many in MongoDB
body: <p>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: </p>
  
  <p><a href="http://i.stack.imgur.com/pLn5N.png" rel="nofollow">Schema relational</a></p>
  
  <p>I have create the follow models:</p>
  
  <p><strong>UserModel</strong></p>
  
  <pre><code>    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);
  </code></pre>
  
  <p><strong>RoleModel</strong></p>
  
  <pre><code> 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);
  </code></pre>
  
  <p><strong>GroupModel</strong></p>
  
  <pre><code>   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);
  </code></pre>
  
  <p><strong>GroupUserRoleModel</strong></p>
  
  <pre><code>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);
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>This OK the implementations?</li>
  <li>When I want create an GroupUserRole document , how do it?</li>
  </ol>
  
  <p>I have seen information about the method populate in mongoose (<a href="http://mongoosejs.com/docs/populate.html" rel="nofollow">Populate</a>) but only there is one relation ship between two models
  Thank for your help.</p>