MongoDB db.dropDatabase() command is used to drop a existing database.This will delete the current database. if you haven't selected any, the default (test) database will be deleted.
Syntax
db.dropDatabase()
In Java to delete a database, first of all, get the object of the required database using the getDatabase() method and delete it by invokimg the drop() method on it.
Example
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; public class DropingDatabase { public static void main( String args[] ) { //Creating a MongoDB client @SuppressWarnings("resource") MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Creating a database mongo.getDatabase("myDatabase"); System.out.println("Databases created successfully"); MongoDatabase database = mongo.getDatabase("myDatabase"); database.drop(); } }
Output
Database deleted successfully