How to delete a MongoDB document using Java?


You can delete a document from an existing collection in MongoDB using the remove() method.

Syntax

db.coll.remove(DELLETION_CRITTERIA)

Where,

  • db is the database.

  • coll is the collection (name) in which you want to insert the document

Example

Assume we have a collection named students in the MongoDB database with the following documents −

{name:"Ram", age:26, city:"Mumbai"}
{name:"Roja", age:28, city:"Hyderabad"}
{name:"Ramani", age:35, city:"Delhi"}

The following query deletes the document with name value as Ram.

> db.test.remove({'name': 'Ram'})
WriteResult({ "nRemoved" : 1 })
> db.test.find()
{ "_id" : ObjectId("5e8700"), "name" : "Roja", "age" : 28, "city" : "Hyderabad" }
{ "_id" : ObjectId("5e4161"), "name" : "Ramani", "age" : 35, "city" : "Delhi" }
{ "_id" : ObjectId("5e8161"), "name" : "Ramani", "age" : 35, "city" : "Delhi" }

Using Java program

In Java, you delete a document from a collection using the in the current collection using the deleteOne() method of the com.mongodb.client.MongoCollection interface.

Therefore to create 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 from which you want to delete the document, using the getCollection() method.

  • Delete the required document invoking the deleteOne() method.

Example

Assume we have a collection named students in the MongoDB database with the following documents −

{name:"Ram", age:26, city:"Mumbai"}
{name:"Roja", age:28, city:"Hyderabad"}
{name:"Ramani", age:35, city:"Delhi"}

Following Java program deletes a document from this collection and displays the remaining −

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
public class DeletingDocuments {
   public static void main( String args[] ) {
      //Creating a MongoDB client
      MongoClient mongo = new MongoClient( "localhost" , 27017 );
      //Connecting to the database
      MongoDatabase database = mongo.getDatabase("myDatabase");
      //Creating a collection
      MongoCollection<Document> collection = database.getCollection("students");
      //Deleting a document
      collection.deleteOne(Filters.eq("name", "Ram"));
      System.out.println("Document deleted successfully...");
      //Retrieving the documents after the delete operation
      FindIterable<Document> iterDoc = collection.find();
      int i = 1;
      System.out.println("Remaining documents:");
      Iterator it = iterDoc.iterator();
      while (it.hasNext()) {
         System.out.println(it.next());
         i++;
      }
   }
}

Output

Document deleted successfully...
Remaining documents:
Document{{_id=5e871141a89ad86b7b8ad229, name=Robert, age=27, city=Vishakhapatnam}}
Document{{_id=5e871141a89ad86b7b8ad22a, name=Rhim, age=30, city=Delhi}}

Updated on: 10-Apr-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements