Mongoose Relations

I am learning Mongodb and I am using Mongoose and I would like to create a very dynamic registration process. I managed to complete the first step of registration using Passport, and everything works fine, I created a user and he is present in the database. Now my registration process will continue, and the user will have to choose a "role": which user are you? There are two options: basic and advanced. Basic has only 3 properties, and advanced has 3 basic properties, plus several others.

I need to expand userSchema or add new fields based on this role, but after a week it still does not work, and I tried a lot of NPM like: mongoose-relationship or mongoose-extension-scheme .

This is basically what I have:

 userSchema = new Schema({ id : Number, email : { type: String, required: true }, role : { type: String, required: true }, profile : { ... } // Profile fields are different for each role. }); // profile 1 basicSchema = new Schema({ info1 : { type: String, required: true }, info2 : { type: String, required: true }, info3 : { type: String, required: true } }); // profile 2 advancedSchema = new Schema({ 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 } }); 

The user already exists, and he is on the screen where he needs to select a role and fill out the selected profile.

For information, I use nodejs and expressjs .

I hope you can help. Thanks.

+6
source share
2 answers

Using mongoose-schema-extend:

Install via npm: $ npm install mongoose-schema-extend

An example of 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 1 var basicSchema = userSchema.extend({ info1 : { type: String, required: true }, info2 : { type: String, required: true }, info3 : { type: String, required: true } }); //profile 2 var 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 } }); 

mongoose-schema-extend

+3
source

Is this what you want. The concept of "inheritance" and the discriminatory field. Give him a chance. I used it and works wonders. Basically you have 3 types of users: basic, advanced and expert. Each of them builds on your base / base model, and then expands with additional properties you need, defining one inheritance for each role.

This gives you many benefits, mainly: mongoose will fill in the appropriate model and remove any validation found in derived models, etc.

https://github.com/briankircho/mongoose-schema-extend

+2
source

All Articles