English 中文(简体)
Java & MongoDB - Drop Database
  • 时间:2024-11-03

Java & MongoDB - Drop Database


Previous Page Next Page  

To drop a databases, you can use MongoDatabse.drop() method to drop the selected databases.


// Creating a Mongo cpent 
MongoCpent mongoCpent = MongoCpents.create();

// connect the database	   
MongoDatabase database = mongoCpent.getDatabase("myDb");

// drop the database
database.drop();

Example

Following is the code snippet to drop a database −


import com.mongodb.cpent.MongoCpent;
import com.mongodb.cpent.MongoCpents;
import com.mongodb.cpent.MongoDatabase;
import com.mongodb.cpent.MongoIterable;

pubpc class Tester {
   pubpc static void main(String[] args) {
      // Creating a Mongo cpent 
      MongoCpent mongoCpent = MongoCpents.create("mongodb://localhost:27017");
      MongoDatabase database = mongoCpent.getDatabase("myDb");

      // drop the database
      database.drop();
      System.out.println("Database dropped.");
      
      // print the databases	   
      MongoIterable<String> pst = mongoCpent.pstDatabaseNames();
      for (String name : pst) {
         System.out.println(name);
      }
   }
}

Now, let s compile and run the above program as shown below.


$javac Tester.java 
$java Tester

Output

On executing, the above program gives you the following output.


Database dropped.
admin
config
local
Advertisements