Java MongoDB Drop Collection Example

This tutorial shows how to drop an existing collection in MongoDB using a Java program.

MongoDB is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document.
Learn MongoDB with Java at https://www.javaguides.net/p/java-mongodb-tutorial.html

Tools and Technologies Used

  • Java (JDK) 10
  • Maven 3.5+
  • Eclipse Neon
  • MongoDB 3.12.0

Installing MongoDB

Use the following article to install MongoDB on Windows 10.
Make sure that you have installed MongoDB and started MongoDB server on default port 27017.

Create Database

Create a new database by using the below command on the MongoDB client terminal:
> use javaguides;
switched to db javaguides
The use command will create a new database if it doesn't exist, otherwise, it will return the existing database.

Java MongoDB Driver

Let's use the following Maven declaration to include the MongoDB Java Driver in our maven project.
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>3.12.0</version>
</dependency>
Note that it is an all-in-one JAR, which embeds the core driver and BSON. BSON, short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments.

Java MongoDB Drop Collection Example

The following example creates the collection named "sampleCollection" and drop same collection using drop() method:
package net.javaguides.mongodb.collection;

import org.bson.Document;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoDropCollection {

    public static void main(String[] args) {

        // Creating a Mongo client
        try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {

            // Accessing the database
            MongoDatabase database = mongoClient.getDatabase("javaguides");

            // create a collection
            database.createCollection("sampleCollection");
            System.out.println("Collections created successfully");

            // get a collection
            MongoCollection < Document > collection = database.getCollection("sampleCollection");
            System.out.println("Collection name -> " + collection);
            // Dropping a Collection
            collection.drop();
            System.out.println("Collection dropped successfully");
        }
    }
}
Output:
Collections created successfully
Collection name -> com.mongodb.client.internal.MongoCollectionImpl@6c64cb25
Collection dropped successfully
A new collection is created with the createCollection() method:
database.createCollection("sampleCollection");
A MongoCollection of documents is created with the getCollection() method:
MongoCollection < Document > collection = database.getCollection("sampleCollection");
A MongoDB collection is dropped using drop() method:
collection.drop();

Java MongoDB Tutorials

All Java MongoDB tutorials at https://www.javaguides.net/p/java-mongodb-tutorial.html

Comments