MongoDB Drop Database


MongoDB Drop Database

The MongoDB drop database operation is used to delete an existing database. This command permanently removes the database and all its collections, making it a powerful and irreversible action.


Syntax

db.dropDatabase()

The db.dropDatabase() method drops the currently selected database.


Example MongoDB Drop Database

Let's look at some examples of how to drop a database in MongoDB:

1. Switch to the Database

use myDatabase

This command switches to the database named myDatabase.

2. Drop the Database

db.dropDatabase()

This command drops the myDatabase database.


Full Example

Let's go through a complete example that includes switching to a database, dropping it, and verifying its deletion.

Step 1: Switch to the Database

This step involves switching to the myDatabase database.

use myDatabase

In this example, we switch to the myDatabase database.

MongoDB Drop Database

Step 2: Drop the Database

This step involves dropping the myDatabase database.

db.dropDatabase()

Here, we drop the myDatabase database.

MongoDB Drop Database

Step 3: Verify the Database Deletion

This step involves listing the databases to verify that myDatabase has been deleted.

show dbs

This command lists all databases. You should not see myDatabase in the list.

MongoDB Drop Database

Conclusion

The MongoDB drop database operation is a critical command for deleting databases. It is essential to use this command carefully, as it permanently removes the database and all its collections.