Diskriminator Mongoose
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var userSchema = new Schema({
name: {
type: String,
required: true,
message: 'Name is required',
},
},{
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
},
discriminatorKey: 'userType',
id: false,
toJSON: {
getters: true,
virtuals: true
},
toObject: {
getters: true,
virtuals: true
}
});
var businessSchema= new Schema({
charges: {
type: Number,
default: 20
},
})
module.exports = mongoose.model('User', userSchema)
module.exports = User.discriminator('Business',businessSchema );
// in business logic/ controller try and debug results
var mongoose = require('mongoose');
const User = mongoose.model("User");
const Business = mongoose.model("Business");
User.findOne({}, (err, user)=>{
console.log(err, user);
})
Business.findOne({}, (err, biz)=>{
console.log(err, biz);
})
Colorful Cow