Mongoose SchemaType.prototype.path Property
The Mongoose SchemaType.prototype.path property of the Mongoose API is used on the SchemaType object. It allows us to get the path or the field of SchemaType in the schema. It can be call on any schema object and is capable of providing path of that particular schema. Let us understand path property using an example.
Syntax:
schemaTypeObject.path( <name> ).path;
Parameters: This property does not accept any parameter.
Return Value: This property returns the path or field name.
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 path property. In this example, we have defined studentSchema having four attributes or fields name, age, rollNumber, and parents. At the end, we are getting the path for name field using path property on schemaTypeObject.path() method.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
parents: {
fatherName: { type: String },
motherName: { type: String }
}
});
const StudentModel = connectionObject.model
( 'Student' , studentSchema);
let path = studentSchema.path( 'name' ).path;
console.log(path);
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
name
Example 2: The below example illustrates the functionality of the Mongoose SchemaType path property. In this example, we have defined studentSchema having four attributes or fields name, age, rollNumber, and parents. In the end, we are getting the path for the nested object property i.e parents.fatherName and parents.motherName using path property.
Filename: app.js
Javascript
const mongoose = require( "mongoose" );
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true ,
useUnifiedTopology: true ,
});
const studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
parents: {
fatherName: { type: String },
motherName: { type: String }
}
});
console.log(studentSchema.path( 'parents.fatherName' ).path);
console.log(studentSchema.path( 'parents.motherName' ).path);
|
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
parents.fatherName
parents.motherName
Reference: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-path
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...