ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

Delete Documents

In this guide, you can learn how to remove documents with the MongoDB Java driver.

You can remove documents by passing a query filter to the deleteOne(), deleteMany() or findOneAndDelete() methods.

The deleteOne() method deletes a single document. If the query filter matches more than one document, the method will remove the first occurrence of a match in the collection.

The deleteMany() method deletes all documents that match the query filter.

The findOneAndDelete() method atomically finds and deletes the first occurrence of a match in the collection.

To specify a collation or hint an index, use DeleteOptions as a second parameter to the deleteOne() and deleteMany() methods.

To specify a collation, hint an index, specify sort order, or specify a projection on the returned document, use FindOneAndDeleteOptions as the second parameter to the findOneAndDelete() method.

The following examples are about a paint store that sells eight different colors of paint. The store had their annual online sale resulting in the following documents in their paint_inventory collection:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 3, "color": "blue", "qty": 0 }
{ "_id": 4, "color": "white", "qty": 0 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 6, "color": "pink", "qty": 0 }
{ "_id": 7, "color": "green", "qty": 0 }
{ "_id": 8, "color": "black", "qty": 8 }

The paint store website displays all documents in the paint_inventory collection. To reduce customer confusion, the store wants to remove the colors that are out of stock.

To remove the out of stock colors, query the paint_inventory collection where the qty is 0 and pass the query to the deleteMany() method:

Bson filter = Filters.eq("qty", 0);
collection.deleteMany(filter);

The following shows the documents remaining in the paint_inventory collection:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 5, "color": "yellow", "qty": 6 }
{ "_id": 8, "color": "black", "qty": 8 }

The store is donating the remaining quantity of their yellow paint. This means that the qty for yellow is now 0 and we need to remove yellow from the collection.

To remove yellow, query the paint_inventory collection where the color is "yellow" and pass the query to the deleteOne() method:

Bson filter = Filters.eq("color", "yellow");
collection.deleteOne(filter);

The following shows the documents remaining in the paint_inventory collection:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 2, "color": "purple", "qty": 8 }
{ "_id": 8, "color": "black", "qty": 8 }

The store would like to raffle the remaining quantity of purple paint and remove purple from the paint_inventory collection.

To pick a color, query the paint_inventory collection where the color is "purple" and pass the query to the findOneAndDelete() method:

Bson filter = Filters.eq("color", "purple");
System.out.println(collection.findOneAndDelete(filter).toJson());

Unlike the other delete methods, findOneAndDelete() returns the deleted document:

{ "_id": 2, "color": "purple", "qty": 8 }

Note

If there are no matches to your query filter, no document gets deleted and the method returns null.

The following shows the documents remaining in the paint_inventory collection:

{ "_id": 1, "color": "red", "qty": 5 }
{ "_id": 8, "color": "black", "qty": 8 }

Note

Example Setup

This example connects to an instance of MongoDB by using a connection URI. To learn more about connecting to your MongoDB instance, see the Create a MongoClient guide. This example also uses the movies collection in the sample_mflix database included in the Atlas sample datasets. You can load them into your database on the free tier of MongoDB Atlas by following the Get Started with Atlas Guide.

The following code is a complete, standalone file that performs a delete one operation and a delete many operation:

// Deletes documents from a collection by using the Java driver
package org.example;
import static com.mongodb.client.model.Filters.eq;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Filters.lt;
public class Delete {
public static void main(String[] args) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("movies");
Bson deleteOneQuery = eq("title", "The Garbage Pail Kids Movie");
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
DeleteResult result = collection.deleteOne(deleteOneQuery);
System.out.println("Deleted document count - delete one: " + result.getDeletedCount());
Bson deleteManyQuery = lt("imdb.rating", 1.9);
// Deletes all documents that have an "imdb.rating" value less than 1.9
result = collection.deleteMany(deleteManyQuery);
// Prints the number of deleted documents
System.out.println("Deleted document count - delete many: " + result.getDeletedCount());
}
}
}
Deleted document count - query for one: 1
Deleted document count - unlimited query: 4

The queries in these examples use the eq() and lt() filters to query documents. For more information about filters, see the Filters Class API documentation.

For more information about the methods and classes used to delete documents, see the following API documentation: