VOOZH about

URL: https://www.geeksforgeeks.org/mongodb/mongoose-schema-connection-prototype-dropdatabase-api/

⇱ Mongoose Schema Connection.prototype.dropDatabase() API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mongoose Schema Connection.prototype.dropDatabase() API

Last Updated : 28 Apr, 2025

The Connection.prototype.dropDatabase() method of the Mongoose API is used on the Connection object. It allows us to delete particular database from MongoDB. Along with the database it will delete collection, documents, and index related data as well. Let us understand dropDatabase() method using an example.

Syntax:

connection.dropDatabase( <callback_function> );

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

  • callback: It is used to specify the callback function which will get executed after the dropDatabase() method ends its execution.

Return Value: If the callback function is not provided this method returns a promise.

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: 

👁 Image
 

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

👁 Image
 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection dropDatabase() method, using callback function.

Filename: app.js

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

node app.js

Output:

true

GUI Representation of the Database using Robo3T GUI tool:

👁 Image
 

Example 2: The below example illustrates the basic functionality of the Mongoose Connection dropDatabase() method, using asynchronous function.

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

👁 Image
 

Filename: app.js

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

node app.js

Output:

true

GUI Representation of the Database using Robo3T GUI tool:

👁 Image
 

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

Comment
Article Tags:

Explore