Open In App

Mongoose Document Model.prototype.remove() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Model.remove() method of the Mongoose API is used to remove the document from the MongoDB database. We can access remove() method on any Model object and on the successful execution of the method that particular document will be removed from the database.

Syntax:

Model.remove() 

Parameters: The Model.remove() method accepts two parameters:

  • options: It is an object with various properties.
  • callback: It is a callback function that will run once execution is completed.

Return Value: The Model.remove() function returns a promise. 

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”. At the end, we are fetching one document object i.e “User1” from database using its ObjectId and removing that document using remove(). In the GUI output we can see document that has “_id” as “63203694182cd3c22ea480ff” has been removed.

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

Javascript




// 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);
  
User.findById("63203694182cd3c22ea480ff").then(doc => {
    doc.remove();
})


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 two columns or fields “name”, and “age”. At the end, we are fetching one document object i.e “User2” from database using its ObjectId and removing that document using remove(). After removing the document, we have fetched same document again and you can see we are getting “null” value. In the GUI output we can see document that has “_id” as “63204ad5182cd3c22ea486ae” has been removed.

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

Javascript




// 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);
  
const removeDocument = async () => {
    const doc = await User.findById("63204ad5182cd3c22ea486ae")
    doc.remove();
    const doc1 = await User.findById("63204ad5182cd3c22ea486ae")
    console.log(doc1)
}
  
removeDocument();


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

node app.js

Output:

null

GUI Representation of the Database using Robo3T GUI tool:

 

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-remove



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