Open In App

Mongoose Query.prototype.get() API

Last Updated : 20 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.get() method of the Mongoose API is used on the Query objects. It allows us to get the latest value for the fields we are updating using mongoose update method. Usually this method is used for update operations to get the updated value for a particular path. Let us understand get() method using an example.

Syntax:

query.get( path );

Parameters: This method accepts a single parameter as described below:

  • path: It is used to specify path or the field to get their latest values.

Return Value: This method returns the Query object.

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: 

 

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

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection get() method. In this example, we are updating a document and modifying the value of age field. At the end using get() method we are getting the latest value for the age field.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = connectionObject.model(
    'Student', studentSchema
);
  
const query = StudentModel.updateOne(
    { name: "Student1" }, { age: 99 }
);
console.log(query.get("age"));


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

node app.js

Output:

99

Example 2: The below example illustrates the basic functionality of the Mongoose Connection get() method. 

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: { type: String, required: true },
    age: Number,
    rollNumber: { type: Number, required: true }
});
  
const StudentModel = connectionObject.model(
    'Student', studentSchema
);
  
const query = StudentModel.findByIdAndUpdate(
    "63a40a1065e8951038a391b1", { rollNumber: 0 }
)
console.log(query.get("rollNumber"));


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

node app.js

Output:

0

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-get



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads