Open In App

Mongoose SchemaTypes schema.path() Function

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose SchemaTypes schema.path() function is used to get the instantiated schema type for a given path. This function can be used to inspect the schema type and validator for a given path.

Syntax: 

schema.path( propertyName );

Parameters: It accepts a single parameter as mentioned above and described below:

  • propertyName: It is the name of the property whose type we want to inspect. 

Return Value: It returns the schema string. 

Setting up Node.js Mongoose Module:

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: The below example illustrates the functionality of the Mongoose SchemaType schema.path() function. In this example, we have defined studentSchema as having three attributes name, Roll Number, and class. And we will inspect the type of all the attributes with the schema.path() function.

Javascript




// Require the mongoose module
const mongoose = require('mongoose');
  
// Path to our cloud DataBase
  
// Connecting to database
mongoose.set('strictQuery', false);
  
mongoose.connect(url)
    .then((ans) => {
        console.log("Connected Successful")
    })
    .catch((err) => {
        console.log("Error in the Connection")
    })
  
// Creating Structure of the model
const studentSchema = new mongoose.Schema({
    name: { type: String },
    rollNumber: { type: Number },
    class: { type: Number },
  
});
  
console.log('Name property accepts: ',
    studentSchema.path('name').instance);
console.log('Roll Number property accepts: ',
    studentSchema.path('rollNumber').instance);
console.log('Class property accepts: ',
    studentSchema.path('class').instance);


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

node script2.js

Output:

output1

Example 2: The below example illustrates the functionality of the Mongoose SchemaType The schema.path() function. In this example, we have defined schema which has an attribute name which is an object that has attributes firstName and lastName. And we will inspect the attributes with the schema.path() function.

Javascript




// Require the mongoose module
const mongoose = require('mongoose');
  
// Path to our cloud DataBase
  
// Connecting to database
mongoose.set('strictQuery', false);
  
mongoose.connect(url)
    .then((ans) => {
        console.log("Connected Successful")
    })
    .catch((err) => {
        console.log("Error in the Connection")
    })
  
// Calling Schema class
const Schema = mongoose.Schema;
  
// Creating Structure of the model
const schema = new Schema({
    name: {
        type: String,
        firstName: {
            type: String,
            require: true,
        },
        lastName: {
            type: String,
            require: true,
        }
    }
});
  
// Compile our model
const Person = mongoose.model('Person', schema);
  
// Inspecting the options using schema.path() function
console.log(schema.path('name').options);


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

node script.js

Output:

output2

Reference: https://mongoosejs.com/docs/schematypes.html#path



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

Similar Reads