Open In App

Mongoose SchemaType.prototype.get() API

Last Updated : 02 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. 

Mongoose SchemaType getters are SchemaType methods that allow us to transform or manipulate data while accessing the data from the DB or schema. Let’s understand the Mongoose SchemaType.prototype.get() method.

Syntax:

mongoose.SchemaType.prototype.get(<getter_function>)

 

Parameters:

  • getter_function: It is the getter function that manipulates or transforms the data in the document.

Return type: This method returns all the required instances of the current schema.

Creating node application And Installing Mongoose:

Step 1: Create a node application using the following command:

mkdir folder_name
cd folder_name
npm init -y
touch main.js

Step 2: After creating the ReactJS application, Install the required module using the following command:

npm install mongoose

Project Structure: It will look like the following.

 

GUI Representation of the  Database using MongoDB Compass: Currently, the collection has no data.

 

Example 1: In this example, we will create a getter function at a particular schema level that will allow us to round down the age to an integer value. 

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
    {
        dbName: 'event_db',
        useNewUrlParser: true,
        useUnifiedTopology: true
    }, err => err ? console.log(err) :
        console.log('Connected to database'));
  
const personSchema = new mongoose.Schema({
    age: {
        type: Number,
        get: function (val) {
            return Math.floor(val)
        }
    }
});
  
const Person = mongoose.model('Person', personSchema);
const person = new Person({ age: 18.4 });
console.log(person.age);


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output: The age 18.4 is converted to 18.

 

GUI Representation of the  Database using MongoDB Compass:

 

Example 2: In this example, we will create a getter function at a particular schema level that will allow us to convert the name into an array of first name and last name. 

Filename: main.js

Javascript




const mongoose = require('mongoose')
  
// Database connection
{
    dbName: 'event_db',
    useNewUrlParser: true,
    useUnifiedTopology: true
}, err => err ? console.log(err) :
   console.log('Connected to database'));
  
const personSchema = new mongoose.Schema({
    name: {
        type: String,
        get: function (val) {
            return val.split(' ')
        }
    }
});
  
const Person = mongoose.model('Person', personSchema);
const person = new Person({ name: 'John Doe' });
console.log(person.name);


Step to Run Application: Run the application using the following command from the root directory of the project:

node main.js

Output: 

 

GUI Representation of the  Database using MongoDB Compass:

 

Reference: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads