Mongoose relationship
Using mongoose-schema-extend:
Install via npm:$ npm install mongoose-schema-extend
Example using your code:
var mongoose = require('mongoose'), extend = require('mongoose-schema-extend');var Schema = mongoose.Schema;var userSchema = new Schema({ id : Number, email : { type: String, required: true }, role : { type: String, required: true },},{ collection : 'users' });//profile 1var basicSchema = userSchema.extend({ info1 : { type: String, required: true }, info2 : { type: String, required: true }, info3 : { type: String, required: true }});//profile 2var advancedSchema = userSchema.extend({ info1 : { type: String, required: true }, info2 : { type: String, required: true }, info3 : { type: String, required: true }, info4 : { type: String, required: true }, info5 : { type: String, required: true }, info6 : { type: String, required: true }});
This is what you want. The concept of "inheritance" and discriminatory field. Give it a shot. I used it and works wonders. Basically you have 3 types of users: basic, advanced and expert. Each is based on your core/base model and then you extend with extra properties you need, defining one inheritance per role.
This gives you a lot of advantages, mainly: mongoose will populate the appropriate model and kick any validation you have in the derived models, etc.