Last Updated: 30 May, 2023
To drop a MongoDB database, we can use the dropDatabase() method.
To drop a database, we need to follow the given steps:
Switch to the database that we want to drop using the use command.
For example, if we want to drop a database named "schoolDB", run the following command:
use schoolDB
Once we have switched to the desired database, we can issue the dropDatabase() command to drop it:
db.dropDatabase()This command will delete the current database we're connected to (in this case, "schoolDB") and remove all its collections and data.
After executing the dropDatabase() command, MongoDB will return a JSON object with the result of the operation. If the database was dropped successfully, we'll see the following output:
{ "dropped": "schoolDB", "ok": 1 }
Please exercise caution when using the dropDatabase() command, as it permanently deletes the entire database and its data. Make sure we have a backup or that we genuinely want to remove all data associated with the database.
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com