Open In App

Mongoose Document prototype.unmarkModified() API

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

The API prototype.unmarkModified() method of the Mongoose API is used to clear the modified state on the specified path of any object. In other words, it is used to unmark the modified values on the document object. We can provide a field value to be unmarked as a parameter to this method.

Syntax:

doc.unmarkModified() 

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

  • path: It is the name of the field in a document in string format, on which you want to unmark the modifications.

Return Value: The prototype.unmarkModified() function returns void.

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”. In the end, we are first getting the document object, post that we setting “10” as value to age field, and using unmarkModified() method and specifying “age” field which I want to unmark modification.

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

App.js




// 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);
  
let doc;
  
const findDoc = async () => {
    doc = await User.findById("63203694182cd3c22ea480ff")
    doc.age = 10
    doc.unmarkModified('age')
    doc.save();
}
findDoc()


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

node app.js

GUI Representation of the  Database using Robo3T GUI tool:

 

Example 2: In this example, we are setting values for two fields of a document object, but we are unmarking modifications for one field. In the image I attached in the result, you will notice only the value for the “age” field is updated but the value for the “name” field is not updated because we unmarked modification for the “name” field.

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

App.js




// 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);
  
let doc;
  
const findDoc = async () => {
    doc = await User.findById("63203694182cd3c22ea480ff")
    doc.age = 10
    doc.name = "User1 Updated"
    doc.unmarkModified('name')
    doc.save();
}
  
findDoc()


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

node app.js

GUI Representation of the  Database using Robo3T GUI tool:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads