Open In App

Mongoose Document Model.prototype.discriminators API

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Model.prototype.discriminators property of the Mongoose API is used to return the registered discriminators for the model.

Syntax:

Model_Name.discriminators

Returns: The Model.prototype.discriminators property returns the registered discriminators for the model.,

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using discriminators property on the User model which will return the registered discriminators of the model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const userSchema = new mongoose.Schema(
    { name: String, age: Number }
)
  
// Defining userSchema model
const User = mongoose.model('User', userSchema);
  
// Setting the discriminator
const ClickedLinkEvent = User.discriminator('ClickedLink',
  new mongoose.Schema({ url: String }));
  
// Getting the discriminator
const output = User.discriminators
console.log("Discriminators is", output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Discriminators is { ClickedLink: Model { ClickedLink } }

Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “father_name”, and “mother_name”. In the end, we are using base property on the Student model which will return the discriminators of the model.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
const studentSchema = new mongoose.Schema({ 
    name: String, 
    father_name: String, 
    mother_name: String 
})
  
// Defining studentSchema model
const Student = mongoose.model('Student', studentSchema);
  
// Setting the Discriminators
const ParentEvent = Student.discriminator(
    'Parent',
      new mongoose.Schema({ parentName: String })
);
    
// Getting the Discriminators
const output = Student.discriminators
console.log("Discriminators is:", output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Discriminators is: { Parent: Model { Parent } }

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-discriminators



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads