Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptMongoose Document.prototype.$ignore() API

Mongoose Document.prototype.$ignore() API

The Document API.prototype.$ignore() method of the Mongoose API is used on the Document model. It allows mongoose to save documents by ignoring some changes to the field of the document. If we set any field to be ignored then changes to that field will be persisted and validation will not be executed on that field. Let us understand the ignore() method using an example.

Syntax:

document.$ignore( path );

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

  • path: It is used to specify the path or the field we want to ignore while inserting or updating that document in the database.

Return Value: This method does not return anything.

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”. At the end, we updated the value of fixedDeposit field for this document and by using ignore() method we are telling mongoose to persist this changes. In the output we can see no changes made in the database.

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: { type: Number, default: 6 }
});
  
const User = mongoose.model('User', userSchema);
  
User.findOne().then(document => {
    document.fixedDeposit = 0;
    document.$ignore('fixedDeposit');
    document.save();
})


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 have established a database connection using mongoose and defined model over userSchema, having five columns or fields “_id”, “name”, “fixedDeposit”, “interest”, and “tenure”. In this example, we modified values of two fields and we are using ignore() method on both the fields to ignore or persist the changes in the database.

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: { type: Number, default: 6 }
});
  
const User = mongoose.model('User', userSchema);
  
User.findById('63917d0fd8465aca8611054e')
    .exec((error, document) => {
        if (error) {
            console.log(error);
        } else {
            document.name = 'Aditya';
            document.fixedDeposit = undefined;
            document.$ignore('name');
            document.$ignore('fixedDeposit');
            document.save();
        }
    })


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-$ignore

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments