Open In App

Mongoose Schema Connection.prototype.close() API

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

The Mongoose Schema API Connection.prototype.close() method of the Mongoose API is used on the Connection objects It allows us to close the mongodb connection from the mongoose side. With the help of this method we can forcefully close the connection. Let us understand close() method using an example.

Syntax:

connection.close( force, callback );

Parameters: This method accepts two parameters as described below:

  • force: It is used to specify whether we want to close the connection forcefully.
  • callback: It is used to specify the callback function, which will get executed after closing the connection.

Return Value: This method return promise if we do not provide any callback function as a parameter to the method.

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 database present in the MongoDB.

 

Example 1: The below example illustrate the functionality of the Mongoose Schema close() method. We can notice that after closing the connection we are not able to create any collection in the database.

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,
})
  
connectionObject.close(true, (error, result) => {
    if (error) {
        console.log('error', error);
    } else {
        console.log('Connection Closed successfully');
    }
})
  
const Customer =
    connectionObject.model('Customer', new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    }));


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

node app.js

Output:

Connection Closed successfully

Example 2: The below example illustrates the functionality of the Mongoose Schema close() method. However, when we comment on the close() method line, we can easily notice that now the collection has been created in the database.

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,
})
  
// connectionObject.close(true, (error, result) => {
//   if(error) {
//     console.log('error', error);
//   } else {
//     console.log('Connection Closed successfully');
//   }
// })
  
const Customer =
    connectionObject.model('Customer', new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    }));


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

node app.js

Output:

Database Structure: The database structure will look like this, the following database present in the MongoDB.

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads