Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose discriminator

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);
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: import downloadcsv from "vue-json-csv"; 
Javascript :: js regex word before word 
Javascript :: add href to image javascript 
Javascript :: jest tocontain 
Javascript :: make alphabet js 
Javascript :: jquery add event to dynamically created element 
Javascript :: having written a counter with redux how does it work 
Javascript :: time zone browser javascript 
Javascript :: select ng-options set default value 
Javascript :: proxmox local storage path 
Javascript :: d3.js on click event 
Javascript :: java.lang.IllegalArgumentException: Can only download HTTP/HTTPS 
Javascript :: find option values using javascript 
Javascript :: create a javascript json object 
Javascript :: console.table in js without index column 
Javascript :: react native refresh control color 
Javascript :: linux command to install standard js 
Javascript :: how to disable menu bar in browser using javascript 
Javascript :: iterate array 
Javascript :: js method .sort 
Javascript :: what is tostring in js 
Javascript :: jquery event delegation 
Javascript :: implement queue using stack javascript 
Javascript :: homepage field in package.json 
Javascript :: print js example 
Javascript :: interactive svg javascript 
Javascript :: how to access node js server from another computer 
Javascript :: angularjs form validation on submit 
Javascript :: array.length in mongoose query 
Javascript :: function create array javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =