Open In App

Mongoose Document prototype.get() API

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The API prototype.get() method of the Mongoose API can be used to get the value of any field of mongoose document object. It can be used on Model object to get the values of attributes present in the collection. 

Syntax:

doc.get()

Parameters: The prototype.get() method accepts three parameters.

  • path: It is the name of the field you want to get value.
  • type: It is used for dynamic casting of the value you are extracting.
  • options: It is an object with various properties.

Return Value: The prototype.get() function returns the value of the field present in database.

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:

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example-1: In this example, We have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. At the end, we are using get() method on the User model object. Firstly, we are getting document object using object Id and then using get() to get the value of “name” field.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
const getValue = async () => {
    const doc = await User.findById(
        "63203694182cd3c22ea480ff")
    const value = doc.get("name")
    console.log(value)
};
  
getValue();


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

User1

Example 2: In this example, we are using get() method on the User model object. We are using promise to get the document object, and on document object we are accessing get() to get the value of “age” field. In the below example, we are casting the result in string format. You can see first console.log() is printing the type of the value which we dynamically typed with the help of get(), and second console.log() is printing the value of “age” field.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
  
User.findById("63203694182cd3c22ea480ff").then(doc => {
    const value = doc.get("age", String);
    console.log(typeof value);
    console.log(value);
});


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

string
10

Reference: https://mongoosejs.com/docs/api/document.html#document_Document-get



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

Similar Reads