Open In App

Mongoose Schema.prototype.virtualpath() Method

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Schema API.prototype.virtualpath() method of the Mongoose API is used on the Schema object. It allows us to get the virtual object we have defined over the schema. This method takes the name of virtual as a parameter and return the VirtualType object with several properties. Let us understand the virtualpath() method using an example.

Syntax:

schemaObject.virtualpath( name );

Parameters: This method accepts a single parameter as described below:

  • name: It is used to specify the virtual name over the schema.

Return Value: This method returns the VirtualType object with various properties like, path, getter, setter etc. If we try to access virtual that we have not defined than we will get null value.

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 Schema virtualpath() method. In this example, we have defined a virtual named numberOfFields on iplTeamSchema which will return number of fields in schema.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const iplTeamSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    number: Number,
    nationality: String,
    iplTeam: String,
    age: Number,
});
  
iplTeamSchema.virtual('numberOfFields').get(() => {
    return ''
})
  
const virtualType = iplTeamSchema.virtualpath("numberOfFields");
console.log(virtualType.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:

numberOfFields

Example 2: The below example illustrates the functionality of the Mongoose Schema virtualpath() method. In this example, we are trying to access the virtual which we have defined and is present in the file, and the virtual we have removed from the file. We can notice that, for the virtual which we have defined and is present in the file we are getting expected result, but for the virtual that we have removed we are getting null value.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const iplTeamSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    number: Number,
    nationality: String,
    iplTeam: String,
    age: Number,
});
  
iplTeamSchema.virtual('databaseInfo').get(() => {
    return 'database name geeksforgeeks schema name iplTeamSchema'
})
  
const virtualType = iplTeamSchema.virtualpath("databaseInfo");
console.log(virtualType.getters[0]());
console.log(iplTeamSchema.virtualpath("numberOfField"))


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

node app.js

Output:

database name geeksforgeeks schema name iplTeamSchema
null

Reference: https://mongoosejs.com/docs/api/schema.html#schema_Schema-virtualpath



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

Similar Reads