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


i developing first database in mongo . m using mongoose create models . want implement multiple relationship 1 many. there 3 models :user, group , role model. user can belong multiple groups , can have several roles in same group. example, john belongs group 1 , 2. juan in group 1 administrator , group 2 administrator , superuser. below show relational schema:

schema relational

i have create 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 implementations?
  2. when want create groupuserrole document , how it?

i have seen information method populate in mongoose (populate) there 1 relation ship between 2 models thank help.

modelization

ok, question here is, want create "relational database"-like using mongodb or want modelize "nosql"-like database ?

i see trying do, reproducing relational schemas. common mistake , here explanation of why should avoid this. using mongodb, there new concepts should aware new relation (one some, many some) some represent group/list of small number of document (we can call sub-document). main benefit of mongodb make less collection if possible.

if want explanation of how subdocument work in mongoose, here link http://mongoosejs.com/docs/subdocs.html

to solve problem, think (if have not many groups , roles suppose), should :

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 have 1 collection 'user' can manage users, groups , roles.

usage

var user = new usermodel({ name: "odonno", groups: [{ name: 'admin' }, { name: 'user' }] }) user.save(callback); 

once have model, can set groups , roles , save in database.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -