jenny jenny - 1 year ago 164
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.

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download