How to Delete a Document in MongoDB using Java

Have a Database Problem? Speak with an Expert for Free
Get Started >>


When you’re working with a database like MongoDB to store and manage your data, there will be times you need to delete certain documents. It’s important to know how to do this correctly, since deleting a document is a permanent action. Fortunately, it’s easy to accomplish this task with Java. In this article, we’ll provide step-by-step instructions for deleting a MongoDB document using Java.


Before we start looking at some code examples, we need to make sure all the system requirements are in place. For this task, there are a few prerequisites that need to be met:

  • You’ll need to confirm that MongoDB, along with the MongoDB Java driver, have been installed and properly configured beforehand.

  • You’ll also need to ensure that the latest Java JDK has been properly installed and configured beforehand.

  • Finally, you’ll need to make sure that the MongoDB service is running.

NOTE: For the purposes of this tutorial, we assume that the MongoDB version used is 4.0 and MongoDB Java Driver is 3.8.2.

The MongoDB Test Data

You won’t want to delete any of your real data when you try out the example code in this tutorial, so it’s a good idea to create a test dataset. Insert the following test documents to your preferred collection name; in this example, the collection will be called “playerInfo”:

IDNumberHero NameNationality

The MongoDB Connection Details

Now that we’ve covered the system requirements and added some test data to MongoDB, we can turn our attention to the Java code. Let’s look at the following code segment:

MongoClient mongo = MongoClients.create("mongodb://");
MongoDatabase db = mongo.getDatabase("assassinDB");
MongoCollection<document> assassinColl = db.getCollection("playerInfo");

In the code shown above, we establish a connection to the MongoDB deployment and access both the database (assassinDB) and the specified collection (playerInfo).

Delete MongoDB Documents and Count the Number of Affected MongoDB Documents within MongoDB Collection

BasicDBObject theQuery = new BasicDBObject();
theQuery.put("nationality", "American");
DeleteResult result = assassinColl.deleteMany(theQuery);
System.out.println("The Numbers of Deleted Document(s) : " + result.getDeletedCount());

Let’s take a moment to discuss what’s going on in this section of code. The following things happen:

  • First, we create a query, and set it to the variable "theQuery". You can see that the condition specified in our query is that the field "nationality" should have the value "American".

  • In the next line, we use the deleteMany() method, passing in "theQuery". This ensures that documents matching the query criteria will be deleted.

  • The getDeletedCount() will return the number of documents affected by the delete operation.

  • After the delete operation occurs, the results should resemble something like this:

The Numbers of Deleted Document(s) : 2

If you look back at the sample dataset, you’ll see that two documents did indeed match the criteria in the query.

Delete MongoDB Documents using the “$gt” operator in BasicDBObject Object

In the next example, we’ll demonstrate how to delete MongoDB documents using the $gt operator.

BasicDBObject regexQuery = new BasicDBObject();
removeQuery.put("number", new BasicDBObject("$gt", "2"));
DeleteResult assassinColl.deleteMany(removeQuery);
System.out.println("The Numbers of Deleted Document(s) : " + result.getDeletedCount());


Let’s review what we did in this section of code:

  • Once again, we created a query, which we set to the variable "regexQuery".

  • This query makes use of the $gt operator. In this case, the query will only look for documents where the field number has a value greater than 3.

  • The deleteMany() method is called, passing in "regexQuery".

  • As in our previous example, the getDeletedCount() is used to return the number of documents affected in the write operation.

  • After the deletion occurs, the results should look something like this:

The Numbers of Deleted Document(s) : 3

Looking back at our sample dataset, we can confirm that three documents met the criteria in the query.

Delete All MongoDB Documents within a Collection in Java using BasicDBObject

There are some cases when you might want to remove all the documents within a given collection:

assassinColl.remove(new BasicDBObject());

In this example, we passed an empty BasicDBObject() in the remove() method. This command will delete all the documents within the stated MongoDB collection.

Delete Entire MongoDB Documents and MongoDB Collection

In our final example, we’ll delete an entire collection, documents and all:


The drop() method used in this example is similar to the “drop” command in SQL, which is mainly used to drop a table in a database. In this case, MongoDB will drop the stated collection and all of its corresponding documents.


Deleting documents is an important task in database management, but it’s a task that needs to be performed with caution. The MongoDB driver for Java makes it easy to delete documents from a MongoDB collection with just a few lines of code; you can choose to delete a single document, many documents or even an entire collection. With the instructions provided in this tutorial, you’ll be ready to handle any example of deleting a MongoDB document using Java.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.