Deleting Databases

Once a database has been created, it exists in MongoDB until an administrator deletes it. Deleting databases is a common task on some systems, especially when databases are created to contain temporary data. It is sometimes easier to delete databases when they become stale and simply create new ones as needed than to try to clean up the entries in a database.

To delete a database from the MongoDB shell, use the dropDatabase() method. For example, to delete the newDB database, you use the following commands to change to the newDB database and then delete it:

use newDB
db.dropDatabase()

You should be aware that dropDatabase() removes the current database, but it does not change the current database handle. This means if you drop a database and then create a collection using the handle without changing the current database first, the dropped database is re-created.

Figure 12.6 shows an example of deleting the newDB from MongoDB.

Image

Figure 12.6 Deleting a database in the MongoDB shell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset