Open In App

Mongoose Schema.prototype.plugin() API

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

The Mongoose Schema API .prototype.plugin() method of the Mongoose API is used on the Schema object. It allows us to create plugins for the schema. With the help of plugins, we can use the same logic for multiple schemas. Let us understand the plugin() method using an example.

Syntax:

schemaObject.plugin( <callback_function>, <options> );

Parameters: This method accepts two parameters as described below:

  • callback: It is used to specify the callback function.
  • options: It is used to identify the various properties.

Return Value: This method does not return any 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 plugin() method. We are accessing the () method on the schema object.

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 customerSchema = new mongoose.Schema({
    name: String,
    address: String,
    orderNumber: Number,
});
  
customerSchema.plugin((schema => { 
    console.log(schema.pathType('address')) }))
  
const Customer = connectionObject
    .model('Customer', customerSchema);


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

node app.js

Output:

real

Example 2: The below example illustrates the functionality of the Mongoose Schema plugin() method. We are accessing the required paths () method on the schema object. This method will return an array of fields required at the schema level.

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 studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
studentSchema.plugin((schemaObject => { 
    console.log(schemaObject.requiredPaths()) }))
  
const StudentModel = connectionObject
    .model('Student', studentSchema);


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

node app.js

Output:

[ 'rollNumber', 'name' ]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads