Mongoose SchemaTypes The type Key
The Mongoose SchemaType has a special property named type. Each property of a Schema needs to have some type. When mongoose sees the nested property named type with a valid value, it assumes that a SchemaType has to be declared with the given type.
Syntax:
Schema({ property : { type: value // other options } });
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 The type key. In this example, we will define the type of property without using the object. And we are getting the accepted data of property in the console.
Javascript
const mongoose = require( 'mongoose' ); // Path to our cloud DataBase const url = 'mongodb://localhost:27017/GFG' // Connecting to database mongoose.set( 'strictQuery' , false ); mongoose.connect(url) .then((ans) => { console.log( "Connected Successful" ) }) . catch ((err) => { console.log( "Error in the Connection" ) }) // Calling Schema class const Schema = mongoose.Schema; // Creating Structure of the model const schema = new Schema({ firstName: String, lastName: String, age: Number, Gender: String, }); // Compile our model const Person = mongoose.model( 'Person' , schema); // Inspecting the options using schema.path() function console.log( 'firstName property is :' , schema.path( 'firstName' ).instance); console.log( 'lastName property is :' , schema.path( 'lastName' ).instance); console.log( 'Age property is :' , schema.path( 'age' ).instance); console.log( 'Gender property is :' , schema.path( 'Gender' ).instance); |
Step to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
firstName property is : String lastName property is : String Age property is : Number Gender property is : String Connected Successful
Example 2: The below example illustrates the functionality of the Mongoose SchemaType The type key. In this example, we have defined the type of nested property with the help of the type key. And see the accepted data type of property in the console.
Javascript
const mongoose = require( 'mongoose' ); // Path to our cloud DataBase const url = 'mongodb://localhost:27017/GFG' // Connecting to database mongoose.set( 'strictQuery' , false ); mongoose.connect(url) .then((ans) => { console.log( "Connected Successful" ) }) . catch ((err) => { console.log( "Error in the Connection" ) }) // Creating Structure of the model const studentSchema = new mongoose.Schema({ name: { type: String }, rollNumber: { type: Number }, class: { type: Number }, }); console.log( 'Name property accepts: ' , studentSchema.path( 'name' ).instance); console.log( 'Roll Number property accepts: ' , studentSchema.path( 'rollNumber' ).instance); console.log( 'Class property accepts: ' , studentSchema.path( 'class' ).instance); |
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 property accepts: String Roll Number property accepts: String Class property accepts: Number Connected Successful
Reference: https://mongoosejs.com/docs/schematypes.html#type-key
Please Login to comment...