Skip to content
Related Articles
Open in App
Not now

Related Articles

Mongoose Document.prototype.$isDeleted() API

Improve Article
Save Article
  • Last Updated : 09 Dec, 2022
Improve Article
Save Article

The Document API.prototype.$isDeleted() method of the Mongoose API is used on the Document model. It allows us to identify whether a document is removed or not. With the help isDeleted() method we can override what mongoose thinks about the document is deleted or not Let us understand isDeleted() method using an example.

Syntax:

document.$isDeleted( value );

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

  • value: It is used to specify the Boolean value. This value overrides what mongoose thinks about the removal of any document.

Return Value: This method returns a Boolean value. If the document is deleted then this method returns true else false.

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 have deleted one document from our collection, later we are accessing the isDeleted() method on the same document object, and we are getting the expected output as true.

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({ name: 'Eric' }).then(document => {
    console.log(document)
    document.remove().then(doc => {
        console.log(doc.$isDeleted());
    });
});


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

node app.js

Output:

{
  _id: new ObjectId("6390f4b9a4ca3e6bc9d41d61"),
  name: 'Eric',
  fixedDeposit: 10000,
  interest: 0.01,
  tenure: 12,
  __v: 0
}
true

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”. At the end, we have deleted one document from our collection, and explicitly overriding what mongoose thinks the document is deleted by passing false value to the isDeleted() method. At the end, we are accessing the isDeleted() method on the same document object, and we are getting expected output as false. Though the document has already been deleted, we can verify the same in 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: Number
});
  
const User = mongoose.model('User', userSchema);
  
const isDeletedExample2 = async () => {
    const document = await User.findOne({ name: 'Chintu' });
    await document.remove();
    document.$isDeleted(false);
    console.log(document.$isDeleted());
}
isDeletedExample2();


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

node app.js

Output:

false

GUI Representation of the Database using Robo3T GUI tool:

 

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!