Open In App

Mongoose Document.prototype.directModifiedPaths() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Document API.prototype.directModifiedPaths() method of the Mongoose API is used on the Document model. It allows us to get the list of fields which we have modified directly or fields which are explicitly modified. Let us understand directModifiedPaths() method using an example.

 Syntax:

document.directModifiedPaths();

Parameters: This method does not accepts any parameter.

Return Value: This method returns list of fields in the form of array of strings.

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 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 five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. We have modified interest and tenure fields and with the help of directModifiedPaths() method we are able to get the paths we directly modified.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: Number
});
  
const User = mongoose.model('User', userSchema);
  
User.findOne().then(document => {
    document.interest = 0.3
    document.tenure = 15
    console.log(document.directModifiedPaths());
});


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

node app.js

Output:

[ 'interest', 'tenure' ]

Example 2: In this example, we have established a database connection using mongoose and defined model over userSchema, having five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. To achieve the functionality of directModifiedPaths() method we have used asynchronous function and using find() method we are getting all the documents from database. At the end, we are using directModifiedPaths() method to get the path we modifed.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    fixedDeposit: Number,
    interest: Number,
    tenure: Number
});
  
const User = mongoose.model('User', userSchema);
  
const directModifiedExample2 = async () => {
    const documents = await User.find();
    documents[0].fixedDeposit = 1000;
    console.log(documents[0].directModifiedPaths());
}
  
directModifiedExample2();


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

node app.js

Output:

[ 'fixedDeposit' ]

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



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