Open In App

Mongoose Schematype

Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is a MongoDB object modeling and handling for node.js environment.

Mongoose Schematype is a configuration for the Mongoose model. Before creating a model, we always need to create a Schema. The SchemaType specifies what type of object is required at the given path. If the object doesn’t match, it throws an error. The SchemaType is declared as follows:

const schema = new Schema({
    name: { type: String },
    age: { type: Number, default: 10 }, 
});

Data Types supported by SchemaType:

  • String
  • Number
  • Date
  • Buffer
  • Boolean
  • Mixed
  • ObjectId
  • Array
  • Decimal128
  • Map
  • Schema

Properties of SchemaType

  • type key: We need to specify the object type that is required to be saved in the document. The type is specified as follows:
const studentSchema = new Schema({
    name: { type: String }, // String type

    // Has a default value of numerical type
    age: { type: Number, default: 8},

    // Containing Date of birth as Date object 
    dob: { type: Date }, 
    skills: [{ type: String }] ,// Array of strings
});
  • options: We can specify different options for a Schema. They are listed as follows:
    • required: It takes a boolean value or a function. If specified with boolean, the required validator is added to it.
    • default: It takes any data type or a function. It sets a default value for the given path.
    • select: It takes a boolean value and specifies the default projection for the queries.
    • validate: It accepts a function which validates the data supplied to the path.
    • get: Define a custom getter function using Object.defineProperty().
    • set: Define a custom setter function using Object.defineProperty().
    • alias: It specifies a virtual name for the get and set functions.
    • immutable: It takes a boolean type of value, and mongoose prevents changing the immutable path until the parent is specified as isNew=true.
    • transform: It takes a function that is used to customize the JSON.stringify() function.
  • Indexes: We can define the indexes for the schema options as follows:
    • index: It takes a boolean type of value and specifies whether the given path is the index or not.
    • unique: It takes a boolean type of value and specifies whether the given path is a unique index or not. If it is true, the index is not required to mention.
    • sparse: It takes a boolean type of value and specifies whether the given path is the sparse index or not.
  • String: The string type has the following functions:
    • lowercase: It takes a boolean type of value and specifies whether to run the toLowerCase() function on the value.
    • uppercase: It takes a boolean type of value and specifies whether to run the toUpperCase() function on the value.
    • trim: It takes a boolean type of value and specifies whether to run the trim() function on the value.
    • match: It has a RegExp which acts as a validator for the value.
    • enum: It takes an array and checks if the value is in the array and creates a validator.
    • minLength: Specifies the minimum length of the string and creates a validator.
    • maxLength: Specifies the maximum length of the string and creates a validator.
    • populate: It takes an object and sets the default to populate options.
  • Number: The number type has the following functions:
    • min: Specifies the minimum value and creates a validator.
    • max: Specifies the maximum value and creates a validator.
    • enum: It takes an array and checks if the value is in the array and creates a validator.
    • populate: It takes an object and sets the default to populate options.
  • Date: The date type has the following functions:
    • min: Specifies the minimum Date and creates a validator.
    • max: Specifies the maximum Date and creates a validator.
    • expires: It creates a TTL index with the value expressed in seconds.
  • ObjectId: It has the populate function which takes an object and sets the default to populate options.

Example: In the following example, we will create a SchemaType for a Student that will contain the fields name, age, skills and date of birth. Then we will save it to MongoDB using mongoose. Node.js and NPM are used in this example, so it is required to be installed.

Step 1: Create a folder and initialize it:

npm init

Step 2: Install mongoose in the project.

npm i mongoose

The project structure is as follows:

 

Step 3: Create a file called index.js. Inside the index.js, connect to MongoDB. Here the MongoDB Compass is used. First, create the Schema, then the Model from Schema, and name it Student. Finally, create a document of the Student model and save it to the database using the save() function.

index.js

 

Step 4: Now run the code using the following command in the Terminal/Command Prompt to run the file.

node index.js

Output: The output for the code is as follows:

 

And the document in the MongoDB is as follows:

 

Reference: https://mongoosejs.com/docs/schematypes.html


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