How to get a list of available Mongoose Discriminators? How to get a list of available Mongoose Discriminators? mongoose mongoose

How to get a list of available Mongoose Discriminators?


Since v4.11.13, mongoose model has model.discriminators which is an array of models, keyed on the name of the discriminator model.

In your case if you do console.log(User.discriminators) you will get:

{  Client: {    ....  },  Employee: {  }}

As far as I can see, this is not documented anywhere.

Line 158 in lib.helpers.model.discriminators.js is where this is created.


I think you want to fetch the names and values of all the discriminators as for the names you can simply use

User.discriminators

but for finding values you can use this

return Promise.all(Object.keys(discriminators).map(i =>     discriminators[i].find({ userId: this._id }))).then(promiseResults =>    promiseResults.reduce((arr, el) => arr.concat(el), []));

you need to put userId under each discriminators for that.