skip skip - 1 year ago 128
AngularJS Question

Mongoose schema for Multi-User application

I need to make schema for a multi user application I need to develop using MongoDB, Express, AngularJS and NodeJS. I have 4 types of users in that application,

. Once a user is registered the application, I need to display forms and information based on the role(s) of a particular user.

Could somebody give me some idea on how to handle the schema for different roles of a user, for I could later use it for role and access level functionalities?

For example, the following
is common for

var registeredUserSchema = mongoose.Schema({
userId: String,
fullName: String ,
email: String,
password: String,
created: { type: Date, default: },
roles: [String]

But then I need a lot of information to be asked from a user with
role so once he has registered to the application, I would like to display a lot many extra information to show him to be filled up in his acocunt information, than a user with just

Could somebody help me with it?

EDIT: More Explaination

For example, a
will have
, but a user with
role is going to have
. So I need to decide on how to structure that data as there is data that common for all the users and then there is data different for each user based on his role. I need help on the strategy to choose to structure that data. For example, in Java Persistence API we have various Inheritance Strategies to structure data in relational database tables.

Answer Source

I'm using this collections for this part :

var permissions_schema = mongoose.Schema({
    name : String,
    title : String
}); // this are mostly static data

var roles_schema = mongoose.Schema({
    name : String,
    title : String,
    _permissions : Array
});// _permissions is an array that contain permissions _id

var contacts_schema = mongoose.Schema({
    mobile : String,
    password : String,
    first_name : String,
    last_name : String,
    _role : Number,
    _enabled : Boolean,
    _deleted : Boolean,
    _verify_code : Number,
    _groups_id : Array,
    _service_id : String
}); // and at last _role is _id of the role which this user owns.

With something like these collections you can manage your users easily. I hope these have good ideas for you.