Open In App

Mongoose SchemaType Options

Mongoose is a common JavaScript library that has a strong interface for operating with MongoDB, a NoSQL database that stores data in JSON-like documents. it’s designed to be easy and versatile, creating it easy to work with MongoDB from your Node.js application.

In Mongoose, a schema type is a property of a schema that defines the type, required, and other characteristics of a field in a MongoDB collection. Schema types are used to validate the data that is stored in the collection and to define how the data should be stored in the database. You can define a schema type in a Mongoose schema by specifying the type of the field and any desired options.



Mongoose schema types have several options that you can use to customize the behavior of your schema. Some of the options available for schema types include:

required: This option specifies whether a value for the field is required. If set to true, the field will be required in all documents. Not specifying this field will by default have a value of false.



default: This option specifies a default value for the field. If a value for the field is not specified in a new document, the default value will be used.

Example 1: In the below example we are creating a Schema which only has a name field that is of type String, is required, has a by the default value of ‘John Doe’, should be unique, and index for this field will be created as the index is set to true, will include a sparse index, the length of the string should be greater than 3, has an alias name called ‘full_name’. Also, this schema value has to get and set functions and when the field is accessed it will change to uppercase, and when the field is set to value will change to lowercase.




// Creating an example schema
// using different schema type options
const mongoose = require('mongoose');
 
// Defining schema
let schemaClass = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        default: 'John Doe',
        unique: true,
        index: true,
        sparse: true,
        validate: (value) => value.length > 3,
        alias: 'full_name',
        get: (value) => value.toUpperCase(),
        set: (value) => value.toLowerCase()
    }
});
// creating model from the schema
let Schema = mongoose.model('Schema', schemaClass);
 
let schema1 = new Schema({
    name: "GeeksForGeeks"
});
// will have a default value of John Doe
let schema2 = new Schema({});    
 
console.log(schema1);
console.log(schema2);

Output:

schema1 output

schema2 output

Example 2: Below example is a schema of a book which have the title of a field that is of type String and is required, author which is of type String and is required, publishedDate which is of type Date and is required, pageCount which is of type Number and is required, a publisher which is of type String, coverImageUrl also of type String.




// Creating an example schema
const mongoose = require('mongoose');
 
// Defining schema
let bookSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    author: {
        type: String,
        required: true
    },
    publishedDate: {
        type: Date,
        required: true
    },
    pageCount: {
        type: Number,
        required: true
    },
    publisher: String,
    coverImageUrl: String
});
// creating model from the schema
let Book = mongoose.model('Book', bookSchema);
 
let book = new Book({
    title: "DSA",
    author: "GeeksForGeeks",
    publishedDate: new Date(),
    pageCount: 512,
});
console.log("Book:", book);

Output:

book


Article Tags :