Open In App

Mongoose Schema API Connection.prototype.deleteModel() Function

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Connection.prototype.deleteModel() method of the Mongoose API is used on the Connection object. It allows us to remove the model from the connection. Using this method we can clean the model associated with the connection. Let us understand deleteModel() method using an example.

Syntax:

connection.deleteModel( name );

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

  • name/regex: It is used to specify the name of the model we want to remove and we provide regular expression than this method will remove all the models whose name matches with regular expression.

Return Value: This method will return the connection object.

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: 

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection deleteModel() method, we are defining Customer model and removing the same using deleteModel(). At the end, we can see no models are present for the connection object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const Customer = connectionObject.model('Customer', new mongoose.Schema({
    name: String,
    address: String,
    orderNumber: Number,
}));
  
console.log(connectionObject.models);
  
const success = connectionObject.deleteModel('Customer');
  
console.log(connectionObject.models);


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

node app.js

Output:

{ Customer: Model { Customer } }
{}

Example 2: The below example illustrates the basic functionality of the Mongoose Connection deleteModel() method. We are defining Customer model and Student model. At the end, we are removing the Student mode and we can see that only Student model has been removed from the connection object Customer model is present for the connection object.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: String,
    age: Number,
    rollNumber: Number
})
  
const customerSchema = new mongoose.Schema({
    name: String,
    address: String,
    orderNumber: Number,
})
  
const Customer = 
    connectionObject.model('Customer', customerSchema);
const Student = 
    connectionObject.model('Student', studentSchema);
  
console.log(connectionObject.models);
  
connectionObject.deleteModel('Student');
  
console.log(connectionObject.models);


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

node app.js

Output:

{ Customer: Model { Customer }, Student: Model { Student } }
{ Customer: Model { Customer } }

Reference: https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads