Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversJava Sync

Delete Documents

On this page

  • Overview
  • Sample Documents
  • Delete Many Documents
  • Delete a Document
  • Find and Delete a Document

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 }

For more information about the methods and classes mentioned in this guide, see the following resources:

←  Insert OperationsModify Documents →