
- 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 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}}
- Related Articles
- How to delete document by _id using MongoDB?
- How to delete a document in MongoDB?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to delete particular data in a document in MongoDB?
- How to insert a document into a MongoDB collection using Java?
- How to insert multiple document into a MongoDB collection using Java?
- How do I delete array value from a document in MongoDB?
- How to update an existing document in MongoDB collection using Java?
- How to delete multiple documents in MongoDB using deleteMany()?
- How to add a sub-document to sub-document array in MongoDB?
- How to delete everything in a MongoDB database?
- How to delete a table from MongoDB database?
- How to delete multiple documents from a collection using Java?
- How to get embedded data in a MongoDB document?
- How to insert a document with date in MongoDB?
