You can delete a single document in a collection with
deleteOne() method uses a query document that you provide
to match the subset of the documents in the collection that match
the query. If you do not provide a query document (or if you provide an
empty document), MongoDB matches all documents in the collection and
deletes the first match.
You can specify additional query options using the
options object passed as the second parameter of the
deleteOne method. You can also pass a
as an optional third parameter. For more information on this method,
deleteOne() API documentation.
If your application requires the deleted document after deletion,
consider using the
method, which has a similar interface to
deleteOne() but also
returns the deleted document.
The following snippet deletes a single document from the
collection. It uses a query document that configures the query
to match movies with a
title value of "Annie Hall".
You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.
If you run the preceding example, you should see the following output:
Successfully deleted one document.
On subsequent runs of the preceding example, as you already deleted the document that matched your query, you should see the following output:
No documents matched the query. Deleted 0 documents.