Mongoose SchemaType.prototype.isRequired API
The Mongoose SchemaType.prototype.isRequired property of the Mongoose API is used on the SchemaType objects. It allows us to verify required property on any field or path defined using mongoose schema. This property does not take any parameter. Let us understand the isRequired property using an example.
Syntax:
schemaTypeObject.path( ... ).isRequired;
Parameters: This property does not accept any parameter.
Return Value: This property returns boolean value. If required property is set true while defining schema this method will return true else if required property is set false while defining schema this method will return false.
Setting up Node.js application:
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: In this example, we are illustrating the functionality of isRequired property by accessing it on SchemaType prototype.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection const URI = "mongodb://localhost:27017/geeksforgeeks" const connectionObject = mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }); const studentSchema = new mongoose.Schema({ name: { type: String, required: true }, age: { type: Number }, rollNumber: { type: Number }, }); const Student = connectionObject.model( 'Student' , studentSchema); console.log(studentSchema.path( 'name' ).isRequired); |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
true
Example 2: In this example, we are illustrating the functionality of isRequired property by accessing it on SchemaType prototype. At the end, if we do not set the required property value for any field while defining mongoose schema then this property will return undefined for that particular field.
Filename: app.js
Javascript
// Require mongoose module const mongoose = require( "mongoose" ); // Set Up the Database connection const URI = "mongodb://localhost:27017/geeksforgeeks" const connectionObject = mongoose.createConnection(URI, { useNewUrlParser: true , useUnifiedTopology: true , }); const studentSchema = new mongoose.Schema({ name: { type: String, required: true }, age: { type: Number, required: false }, rollNumber: { type: Number }, }); const Student = connectionObject.model( 'Student' , studentSchema); console.log(studentSchema.path( 'name' ).isRequired); console.log(studentSchema.path( 'age' ).isRequired); console.log(studentSchema.path( 'rollNumber' ).isRequired); |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
true false undefined
Reference: https://mongoosejs.com/docs/api.html#schematype_SchemaType-isRequired
Please Login to comment...