jenny jenny - 2 months ago 14
Node.js Question

How to override parent schema field with discriminators using mongoosejs

I have a parent schema Post:

title: {
type: String,
required: true
authors: {
type: [String],
required: true

I want to make a child schema inheriting on this using the discriminator() mongoose function, but I want to add extra validation on the authors field to make sure the array it not empty.

How can I do this?

Also, could anyone point me to good documentation on mongoose discriminators. The official documentation hasn't been helpful in my search.


Would this help you?

var Parent = mongoose.model('Parent', new mongoose.Schema({
    title: {...},
    author: {...}

var Child = Parent.discriminator('Child', new mongoose.Schema({
    author: {
        default: ['something']

Just be aware that you have to include Parent before Child (apparently :o)