
- 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 delete multiple documents from a collection using Java?
In Java the com.mongodb.client.MongoCollection interface provides a method deleteMany(). Using this method you can delete multiple documents from a collection at once, to this method you need to pass the filter specifying the deletion criteria.
Example
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import org.bson.conversions.Bson; import com.mongodb.MongoClient; public class DeletingMultipleDocuments { 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 object MongoCollection<Document>collection = database.getCollection("students"); Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Delhi"); Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam"); Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi"); Document document4 = new Document("name", "Radha").append("age", 45).append("city", "Delhi"); Document document5 = new Document("name", "Sarmista").append("age", 35).append("city", "Mumbai"); //Inserting the created documents List<Document> list = new ArrayList<Document>(); list.add(document1); list.add(document2); list.add(document3); list.add(document4); list.add(document5); collection.insertMany(list); System.out.println("Documents Inserted"); System.out.println("Contents of the collection:"); 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++; } collection = database.getCollection("students"); //Deleting multiple documents Bson filter = new Document("city", "Delhi"); collection.deleteMany(filter); System.out.println("Document deleted successfully..."); //Retrieving the documents after the delete operation iterDoc = collection.find(); System.out.println("Remaining documents:"); it = iterDoc.iterator(); while (it.hasNext()) { System.out.println(it.next()); i++; } } }
Output
Documents Inserted Contents of the collection: Remaining documents: Document{{_id=5e896b042e4a2f3badebddae, name=Ram, age=26, city=Delhi}} Document{{_id=5e896b042e4a2f3badebddaf, name=Robert, age=27, city=Vishakhapatnam}} Document{{_id=5e896b042e4a2f3badebddb0, name=Rhim, age=30, city=Delhi}} Document{{_id=5e896b042e4a2f3badebddb1, name=Radha, age=45, city=Delhi}} Document{{_id=5e896b042e4a2f3badebddb2, name=Sarmista, age=35, city=Mumbai}} Document deleted successfully... Remaining documents: Document{{_id=5e896b042e4a2f3badebddaf, name=Robert, age=27, city=Vishakhapatnam}} Document{{_id=5e896b042e4a2f3badebddb2, name=Sarmista, age=35, city=Mumbai}}
- Related Articles
- How to delete documents from a collection in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- How to delete all the documents from a collection in MongoDB?
- Delete data from a collection in MongoDB using multiple conditions?
- How to delete multiple documents in MongoDB using deleteMany()?
- How to retrieve all the documents from a MongoDB collection using Java?
- How to sort the documents of a MongoDB collection using java?
- How to retrieve documents from a collection in MongoDB?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to insert multiple document into a MongoDB collection using Java?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to Delete Item from Hashtable Collection in C#?
- How to remove all documents from a collection except a single document in MongoDB?
- Delete a collection from MongoDB with special characters?
- How to skip documents while retrieving data from MongoDB, using java?

Advertisements