Docs Menu
Docs Home
/ / /
Node.js Driver
/ /

Delete a Document

You can delete a single document in a collection with collection.deleteOne(). The 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 more query options using the options object passed as the second parameter of the deleteOne method. For more information on this method, see the deleteOne() API documentation.

Note

If your application requires the deleted document after deletion, consider using the collection.findOneAndDelete() method, which has a similar interface to deleteOne() but also returns the deleted document.

The following snippet deletes a single document from the movies collection. It uses a query document that configures the query to match movies with a title value of "Annie Hall".

Note

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.

1// Delete a document
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete the first document in the "movies" collection that matches
16 the specified query document */
17 const query = { title: "Annie Hall" };
18 const result = await movies.deleteOne(query);
19
20 /* Print a message that indicates whether the operation deleted a
21 document */
22 if (result.deletedCount === 1) {
23 console.log("Successfully deleted one document.");
24 } else {
25 console.log("No documents matched the query. Deleted 0 documents.");
26 }
27 } finally {
28 // Close the connection after the operation completes
29 await client.close();
30 }
31}
32// Run the program and print any thrown exceptions
33run().catch(console.dir);
1// Delete a document
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Delete the first document in the "movies" collection that matches
16 the specified query document */
17 const query = { title: "Annie Hall" };
18 const result = await movies.deleteOne(query);
19
20 /* Print a message that indicates whether the operation deleted a
21 document */
22 if (result.deletedCount === 1) {
23 console.log("Successfully deleted one document.");
24 } else {
25 console.log("No documents matched the query. Deleted 0 documents.");
26 }
27 } finally {
28 // Close the connection after the operation completes
29 await client.close();
30 }
31}
32// Run the program and print any thrown exceptions
33run().catch(console.dir);

Note

Identical Code Snippets

The JavaScript and TypeScript code snippets above are identical. There are no TypeScript specific features of the driver relevant to this use case.

Running the preceding example, you see the following output:

Successfully deleted one document.

If you run the example more than once, you see the following output because you deleted the matching document in the first run:

No documents matched the query. Deleted 0 documents.

Back

Delete