Discriminators

The model.discriminator() function

Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.

Suppose you wanted to track different types of events in a single collection. Every event will have a timestamp, but events that represent clicked links should have a URL. You can achieve this using the model.discriminator() function. This function takes 3 parameters, a model name, a discriminator schema and an optional key (defaults to the model name). It returns a model whose schema is the union of the base schema and the discriminator schema.

  1. const options = { discriminatorKey: 'kind' };
  2. const eventSchema = new mongoose.Schema({ time: Date }, options);
  3. const Event = mongoose.model('Event', eventSchema);
  4. // ClickedLinkEvent is a special type of Event that has
  5. // a URL.
  6. const ClickedLinkEvent = Event.discriminator('ClickedLink',
  7. new mongoose.Schema({ url: String }, options));
  8. // When you create a generic event, it can't have a URL field...
  9. const genericEvent = new Event({ time: Date.now(), url: 'google.com' });
  10. assert.ok(!genericEvent.url);
  11. // But a ClickedLinkEvent can
  12. const clickedEvent = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
  13. assert.ok(clickedEvent.url);

Discriminators save to the Event model’s collection

Suppose you created another discriminator to track events where a new user registered. These SignedUpEvent instances will be stored in the same collection as generic events and ClickedLinkEvent instances.

  1. const event1 = new Event({ time: Date.now() });
  2. const event2 = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
  3. const event3 = new SignedUpEvent({ time: Date.now(), user: 'testuser' });
  4. await Promise.all([event1.save(), event2.save(), event3.save()]);
  5. const count = await Event.countDocuments();
  6. assert.equal(count, 3);

Discriminator keys

The way mongoose tells the difference between the different discriminator models is by the ‘discriminator key’, which is __t by default. Mongoose adds a String path called __t to your schemas that it uses to track which discriminator this document is an instance of.

  1. const event1 = new Event({ time: Date.now() });
  2. const event2 = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
  3. const event3 = new SignedUpEvent({ time: Date.now(), user: 'testuser' });
  4. assert.ok(!event1.__t);
  5. assert.equal(event2.__t, 'ClickedLink');
  6. assert.equal(event3.__t, 'SignedUp');