Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to drop a database in MongoDB using java?
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
Advertisements