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.
db.dropDatabase()
The db.dropDatabase()
method drops the currently selected database.
Let's look at some examples of how to drop a database in MongoDB:
use myDatabase
This command switches to the database named myDatabase
.
db.dropDatabase()
This command drops the myDatabase
database.
Let's go through a complete example that includes switching to a database, dropping it, and verifying its deletion.
This step involves switching to the myDatabase
database.
use myDatabase
In this example, we switch to the myDatabase
database.
This step involves dropping the myDatabase
database.
db.dropDatabase()
Here, we drop the myDatabase
database.
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.
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.