Delete Documents
On this page
Overview
In this guide, you can learn how to use the Java Reactive Streams driver to delete documents from a MongoDB collection by performing delete operations.
A delete operation deletes one or more documents from a MongoDB collection.
You can perform a delete operation by using the deleteOne()
or
deleteMany()
methods.
Sample Data
The examples in this guide use the sample_restaurants.restaurants
collection
from the Atlas sample datasets.
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started tutorial.
Important
Project Reactor Library
This guide uses the Project Reactor library to consume Publisher
instances returned
by the Java Reactive Streams driver methods. To learn more about the Project Reactor library
and how to use it, see Getting Started
in the Reactor documentation. To learn more about how we use Project Reactor
library methods in this guide, see the Write Data to MongoDB guide.
Delete Operations
You can perform delete operations in MongoDB by using the following methods:
deleteOne()
, which deletes the first document that matches the search criteriadeleteMany()
, which deletes all documents that match the search criteria
Each delete method requires a query filter, which specifies the search criteria that determine which documents to select for removal. For more information about query filters, see the Query Filter Documents section in the MongoDB Server manual.
Delete One Document
To delete a single document from a MongoDB collection, call the deleteOne()
method and pass in your query filter. Then, pass the deleteOne()
result to the static
Mono.from
method from Mono
. Mono
is a class from the Project Reactor library. In Java Reactive Streams,
the driver methods return cold Publisher
instances, which means that the
corresponding operation does not happen unless you subscribe to the returned
Publisher
. This guide uses the Project Reactor library to consume them. To learn more
about Mono
, see Mono in the
Project Reactor documentation.
The following example deletes a document from the restaurants
collection
that has a name
value of "Ready Penny Inn"
:
Publisher<DeleteResult> deletePublisher = restaurants.deleteOne( eq("name", "Ready Penny Inn")); Mono.from(deletePublisher).block();
Delete Multiple Documents
To delete multiple documents from a MongoDB collection, call the deleteMany()
method and pass in your query filter. Then, pass the deleteMany()
result to the static
Mono.from
method from Mono
. Mono
is a class from the Project Reactor library. In Java Reactive Streams,
the driver methods return cold Publisher
instances, which means that the
corresponding operation does not happen unless you subscribe to the returned
Publisher
. This guide uses the Project Reactor library to consume them. To learn more
about Mono
, see Mono in the
Project Reactor documentation.
The following example removes all documents in the restaurants
collection
that have a borough
value of "Brooklyn"
:
Publisher<DeleteResult> deletePublisher = restaurants.deleteMany( eq("borough", "Brooklyn")); Mono.from(deletePublisher).block();
Customize the Delete Operation
The DeleteOptions
class contains methods that modify
the behavior of delete methods. To use the DeleteOptions
class, construct a new instance of the class, then call one or more of its methods
to modify the delete operation. You can chain these method calls together.
To modify the behavior of the delete operation, pass the class instance and
chained method calls as the second argument to the
deleteOne()
or deleteMany()
method.
You can use the following methods in the DeleteOptions
class
to modify a delete method. All methods are optional.
Method | Description |
---|---|
collation (Collation collation) | Specifies the kind of language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
hint (Bson hint) | Gets or sets the index to scan for documents.
For more information, see the hint statement
in the MongoDB Server manual. |
hint (String hint) | Gets or sets the index to scan for documents.
For more information, see the hint statement
in the MongoDB Server manual. |
let (Bson variables) | A map of parameter names and values. Values must be constant or closed
expressions that don't reference document fields. For more information,
see the let statement in the
MongoDB Server manual. |
comment (BsonValue comment) | A comment to attach to the operation. For more information, see the delete command
fields guide in the
MongoDB Server manual for more information. |
comment (String comment) | A comment to attach to the operation. For more information, see the delete command
fields guide in the
MongoDB Server manual for more information. |
Example
The following code uses the deleteMany()
method to delete all documents in
the restaurants
collection with a name
value that includes the string "Mongo"
.
It also uses the comment
method to add a comment to the operation.
Publisher<DeleteResult> deletePublisher = restaurants.deleteMany( regex("name", "Mongo"), new DeleteOptions().comment("Deleting Mongo restaurants")); Mono.from(deletePublisher).block();
Additional Information
For runnable code examples of inserting documents with the Java Reactive Streams driver, see the Write Data to MongoDB guide.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: