Open In App

Mongoose Document Model.inspect() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Model.inspect() method of the Mongoose API is used to get the model’s name in the database. This method works on any model object which is defined using mongoose.

Syntax:

Model.inspect()

Parameters: The Model.inspect() method does not accept any parameter.

Return Value: The Model.inspect() function returns string. The result contains the model name.

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 Model.inspect() method on the User model that returns the model name in the String format.

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 model = mongoose.model('User', userSchema);
 
const output = model.inspect();
console.log(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:

Model { 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”, “class”, and “school”. In the end, we are using Model.inspect() method on the Student model which will return the string having the name 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, class: Number, school: String }
)
 
// Defining userSchema model
const model = mongoose.model('Student', studentSchema);
 
const output = model.inspect();
console.log(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:

Model { Student }

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



Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads