Model.discriminator()

Parameters
  • name «String» discriminator model name

  • schema «Schema» discriminator model schema

  • [value] «String» the string stored in the discriminatorKey property. If not specified, Mongoose uses the name parameter.

Returns:
  • «Model» The newly created discriminator model

Adds a discriminator type.

Example:

  1. function BaseSchema() {
  2. Schema.apply(this, arguments);
  3. this.add({
  4. name: String,
  5. createdAt: Date
  6. });
  7. }
  8. util.inherits(BaseSchema, Schema);
  9. const PersonSchema = new BaseSchema();
  10. const BossSchema = new BaseSchema({ department: String });
  11. const Person = mongoose.model('Person', PersonSchema);
  12. const Boss = Person.discriminator('Boss', BossSchema);
  13. new Boss().__t; // "Boss". `__t` is the default `discriminatorKey`
  14. const employeeSchema = new Schema({ boss: ObjectId });
  15. const Employee = Person.discriminator('Employee', employeeSchema, 'staff');
  16. new Employee().__t; // "staff" because of 3rd argument above