Open In App

Mongoose Schema.prototype.virtuals Property

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Schema API.prototype.virtuals property of the Mongoose API is used on the Schema object. It allows us to get information about virtual we have defined over schema. It is represented in the form of objects where keys are virtual paths we have defined over schema. Let us understand virtuals property using an example.

Syntax:

schemaObject.virtuals;

Parameters: This property does not accept any parameter.

Return Value: This property returns an object called as VirtualType which will have various properties.

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 virtuals property. In this example, we have defined a virtual named schemaName on kidsSchema which will return a string. At the end, using virtuals property we are getting the object named VirtualType.

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 kidsSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
});
  
kidsSchema.virtual('schemaName').get(() => {
    return 'Kids'
});
  
const virtualType = kidsSchema.virtuals.schemaName;
console.log(virtualType);


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

node app.js

Output:

VirtualType {
  path: 'schemaName',
  getters: [ [Function (anonymous)] ],
  setters: [],
  options: {}
}

Example 2: The below example illustrates the functionality of the Mongoose Schema virtuals property. In this example, we are getting the value we are returning from the virtual.

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 6;
})
  
const virtualType = iplTeamSchema.virtuals.numberOfFields.getters[0]();
console.log('Number of Fields in iplTeamSchema are -', virtualType);


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

node app.js

Output:

Number of Fields in iplTeamSchema are - 6

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



Last Updated : 23 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads