
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to drop a MongoDB Collection using Java?
You can drop an existing collection from MongoDB using the drop() method.
Syntax
db.coll.drop()
Where,
db is the database.
coll is the collection (name) in which you want to insert the document
Example
Assume we have created 3 collections in a MongoDB database as shown below −
> use sampleDatabase switched to db sampleDatabase > db.createCollection("students") { "ok" : 1 } > db.createCollection("teachers") { "ok" : 1 } > db.createCollection("sample") { "ok" : 1 } > show collections sample students teachers
The following query deletes the collection named sample.
> db.sample.drop() true > show collections example students teachers
Using Java program
In Java, you can drop a collection using the in the current collection using the drop() method of the com.mongodb.client.MongoCollection interface.
Therefore to drop a collection in MongoDB using Java program −
Make sure you have installed MongoDB in your system
Add the following dependency to its pom.xml file of your Java project.
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.2</version> </dependency>
Create a MongoDB client by instantiating the MongoClient class.
Connect to a database using the getDatabase() method.
Get the object of the collection you want to drop, using the getCollection() method.
Drop the collection by invoking the drop() method.
Example
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoIterable; import com.mongodb.MongoClient; public class DropingCollection { public static void main( String args[] ) { //Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("mydatabase"); //Creating multiple collections database.createCollection("sampleCollection1"); database.createCollection("sampleCollection2"); database.createCollection("sampleCollection3"); database.createCollection("sampleCollection4"); //Retrieving the list of collections MongoIterable<String> list = database.listCollectionNames(); System.out.println("List of collections:"); for (String name : list) { System.out.println(name); } database.getCollection("sampleCollection4").drop(); System.out.println("Collection dropped successfully"); System.out.println("List of collections after the delete operation:"); for (String name : list) { System.out.println(name); } } }
Output
List of collections: sampleCollection4 sampleCollection1 sampleCollection3 sampleCollection2 Collection dropped successfully List of collections after the delete operation: sampleCollection1 sampleCollection3 sampleCollection2
- Related Articles
- How to drop a collection in MongoDB?
- How to drop a numeric collection from MongoDB?
- How to drop a database in MongoDB using java?
- How to create a MongoDB collection using Java?
- Drop Collection if already exists in MongoDB using Python
- How to drop an index in MongoDB using Java?
- How to insert a document into a MongoDB collection using Java?
- How to sort the documents of a MongoDB collection using java?
- How to Update multiple documents in a MongoDB collection using Java?
- How to insert multiple document into a MongoDB collection using Java?
- How to retrieve all the documents from a MongoDB collection using Java?
- How to update an existing document in MongoDB collection using Java?
- How to update MongoDB collection using $toLower?
- How can I drop a collection in MongoDB with two dashes in the name?
- How to drop a database in MongoDB?
