Docs 菜单
Docs 主页
/ / /
Node.js 驱动程序
/

Delete Documents

在本节中,我们将向您展示如何调用写入操作,以从 MongoDB database 的集合中删除文档。

如果要从集合中删除现有文档,可以使用 deleteOne()删除一个文档,或使用deleteMany()删除一个或多个文档。 这些方法接受与要删除的文档相匹配的查询文档。

注意

如果应用程序在删除后使用有关已删除文档的信息,则可以使用 集合.findOneAndDelete()方法,该方法具有与 deleteOne() 类似的接口,但也会返回已删除的文档。

您可以通过 JSON 对象中的deleteOne()deleteMany()写入操作指定要删除的一个或多个文档,如下所示:

const doc = {
pageViews: {
$gt: 10,
$lt: 32768
}
};

要使用deleteOne()方法删除第一个匹配文档,或使用deleteMany()方法删除所有匹配文档,请将文档作为方法参数传递:

const deleteResult = await myColl.deleteOne(doc);
const deleteManyResult = await myColl.deleteMany(doc);

您可以通过访问上述每个方法调用结果的deletedCount字段来输出该操作删除的文档数量,如下所示:

console.dir(deleteResult.deletedCount);
console.dir(deleteManyResult.deletedCount);

如果删除操作成功,这些声明会打印关联操作删除的文档数。

注意

设置示例

此示例使用连接 URI 连接到MongoDB实例。要学习;了解有关连接到MongoDB实例的更多信息,请参阅 连接到MongoDB指南。此示例还使用Atlas示例数据集包含的 sample_mflix数据库中的 movies集合。您可以按照Atlas入门指南,将它们加载到MongoDB Atlas免费套餐上的数据库中。

注意

无Typescript特定功能

以下代码示例使用JavaScript。驾驶员没有与此使用案例相关的Typescript特定功能。

以下代码是一个完整的独立运行文件,用于执行删除一个操作:

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);

运行上述示例将产生以下输出:

Successfully deleted one document.

如果多次运行该示例,则代码会生成以下输出,因为您在第一次运行中删除了匹配的文档:

No documents matched the query. Deleted 0 documents.

注意

设置示例

此示例使用连接 URI 连接到MongoDB实例。要学习;了解有关连接到MongoDB实例的更多信息,请参阅连接到MongoDB指南。此示例还使用Atlas示例数据集包含的 sample_mflix数据库中的 movies集合。您可以按照Atlas入门指南,将它们加载到MongoDB Atlas免费套餐上的数据库中。

以下代码是一个完整的独立运行文件,用于执行删除多个操作:

1// Delete multiple documents
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 all documents that match the specified regular
16 expression in the title field from the "movies" collection */
17 const query = { title: { $regex: "Santa" } };
18 const result = await movies.deleteMany(query);
19
20 // Print the number of deleted documents
21 console.log("Deleted " + result.deletedCount + " documents");
22 } finally {
23 // Close the connection after the operation completes
24 await client.close();
25 }
26}
27// Run the program and print any thrown exceptions
28run().catch(console.dir);
1// Delete multiple documents
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 all documents that match the specified regular
16 expression in the title field from the "movies" collection */
17 const result = await movies.deleteMany({ title: { $regex: "Santa" } });
18
19 // Print the number of deleted documents
20 console.log("Deleted " + result.deletedCount + " documents");
21 } finally {
22 // Close the connection after the operation completes
23 await client.close();
24 }
25}
26// Run the program and print any thrown exceptions
27run().catch(console.dir);

首次运行上述示例会产生以下输出:

Deleted 19 documents

如果多次运行该示例,您将看到以下输出,因为您在第一次运行时删除了匹配的文档:

Deleted 0 documents

要进一步了解本指南所讨论的任何类型或方法,请参阅以下 API 文档:

后退

更新数组

在此页面上