VOOZH about

URL: https://www.geeksforgeeks.org/node-js/mongoose-document-model-prototype-basemodelname-api/

⇱ Mongoose Document Model.prototype.baseModelName API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mongoose Document Model.prototype.baseModelName API

Last Updated : 15 Sep, 2022

The Model.prototype.baseModelName property of the Mongoose API is used to return the baseModelName as the name of the base model, if this is a discriminator model,

Syntax:

Model_Name.baseModelName

Return Value: The Model.prototype.baseModelName property returns the baseModelName is the name of the base 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:

👁 Image
 

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 baseModelName property on the User model which will return the model name of the instance used by the model.

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

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

node app.js

Output:

baseModelName is User

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 the base property on the Student model which will return baseModelName of the model.

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

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

node app.js

Output:

Base Model Name is: Student

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

Comment

Explore