I'm studying Mongodb and I'm using Mongoose and I would like to create a very dynamic registration process. I managed the first step of the registration using Passport and everything works fine, I created the user and it is present on the DB. Now my registration process will continue and user have to select a "role": What kind of user you are? There are 2 options: "basic" and "advanced". Basic has just 3 properties and advanced has the 3 basic properties plus few others.
I need to extend the userSchema
or to add new fields based on that role but after a week it doesn't work yet and I tried many NPM
like: mongoose-relationship or mongoose-schema-extend.
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 exist and he is on a screen where he need to choose a role and populate the chosen profile.
For information I'm using nodejs and expressjs.
I hope you can help. Thanks.
This is a one to one relationship. everything is unique on both sides there really isn't a need for more than one collection. Instead we can nest one type of data in the other. const mongoose = require("mongoose") const Owner = new mongoose. Schema({ name: String }) const houseSchema = new mongoose.
You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With