Java MongoDB Delete Document Example

This tutorial shows how to delete a document in the 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.

Database Setup

In the previous tutorial, we have created MongoDB database, collection and inserts few documents into a collection.

Java MongoDB Driver

We 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 Delete Document Example - deleteOne() Method

In the following example, we delete a document "_id" = 1:
package net.javaguides.mongodb.document;

import java.util.ArrayList;

import org.bson.Document;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

/**
 * MongoDB Delete Documents Example
 * @author Ramesh Fadatare
 *
 */

public class MongoDeleteDocument {

    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");

            // Retieving a collection
            MongoCollection < Document > collection = database.getCollection("users");

            // Deleting the documents
            collection.deleteOne(Filters.eq("_id", 1));
            System.out.println("Document deleted successfully...");

            try (MongoCursor < Document > cur = collection.find().iterator()) {

                while (cur.hasNext()) {

                    var doc = cur.next();
                    var users = new ArrayList < > (doc.values());

                    System.out.printf("%s: %s%n", users.get(1), users.get(2));
                }
            }

        }
    }
}
Output:
Document deleted successfully...
Tony: Stark
Tom: Cruise
Amir: Khan
Umesh: Fadatare
The deleteOne() deletes the document of "Ramesh". The eq() creates a filter that matches all documents where the value of the field name equals the specified value.
collection.deleteOne(Filters.eq("_id", 1))

Java MongoDB Tutorials

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

Comments