Overview
在本节中,我们将向您展示如何调用写入操作,以从 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);
如果删除操作成功,这些声明会打印关联操作删除的文档数。
deleteOne() 示例:完整文件
注意
设置示例
This example connects to an instance of MongoDB by using a connection URI. To learn more about connecting to your MongoDB instance, see the Connect to MongoDB guide. This example also uses the movies collection in the sample_mflix database included in the Atlas sample datasets. You can load them into your database on the free tier of MongoDB Atlas by following the MongoDB Get Started.
注意
无Typescript特定功能
以下代码示例使用JavaScript。驱动程序没有与此使用案例相关的 TypeScript 特定功能。
以下代码是一个完整的独立运行文件,用于执行删除一个操作:
1 // Delete a document 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async 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 33 run().catch(console.dir);
运行上述示例将产生以下输出:
Successfully deleted one document.
如果多次运行该示例,则代码会生成以下输出,因为您在第一次运行中删除了匹配的文档:
No documents matched the query. Deleted 0 documents.
deleteMany() 示例:完整文件
注意
设置示例
This example connects to an instance of MongoDB by using a connection URI. To learn more about connecting to your MongoDB instance, see the Connect to MongoDB guide. This example also uses the movies collection in the sample_mflix database included in the Atlas sample datasets. You can load them into your database on the free tier of MongoDB Atlas by following the MongoDB Get Started.
以下代码是一个完整的独立运行文件,用于执行删除多个操作:
1 // Delete multiple documents 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string. 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async 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 28 run().catch(console.dir);
1 // Delete multiple documents 2 3 import { MongoClient } from "mongodb"; 4 5 // Replace the uri string with your MongoDB deployment's connection string 6 const uri = "<connection string uri>"; 7 8 const client = new MongoClient(uri); 9 10 async 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 27 run().catch(console.dir);
首次运行上述示例会产生以下输出:
Deleted 19 documents
如果多次运行该示例,您将看到以下输出,因为您在第一次运行时删除了匹配的文档:
Deleted 0 documents
API 文档
要进一步了解本指南所讨论的任何类型或方法,请参阅以下 API 文档: