Overview
在本节中,我们将向您展示如何调用写入操作,以从 MongoDB database 的集合中删除文档。
删除
如果要删除collection中的现有文档,可以使用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() 示例:完整文件
注意
设置示例
该示例使用连接 URI 连接到 MongoDB 实例。如需了解有关连接到 MongoDB 实例的更多信息,请参阅连接到 MongoDB 指南。本示例还使用了Atlas 示例数据集中包含的 sample_mflix 数据库中的 movies 集合。您可以按照 MongoDB 入门指南,将它们加载到 MongoDB Atlas 的免费集群数据库中。
注意
无Typescript特定功能
以下代码示例使用JavaScript。驱动程序没有与此使用案例相关的 TypeScript 特定功能。
以下代码是一个完整的独立运行文件,用于执行删除一个操作:
// Delete a document
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection stringconst uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() { try { const database = client.db("sample_mflix"); const movies = database.collection("movies");
/* Delete the first document in the "movies" collection that matches the specified query document */ const query = { title: "Annie Hall" }; const result = await movies.deleteOne(query);
/* Print a message that indicates whether the operation deleted a document */ if (result.deletedCount === 1) { console.log("Successfully deleted one document."); } else { console.log("No documents matched the query. Deleted 0 documents."); } } finally { // Close the connection after the operation completes await client.close(); }}// Run the program and print any thrown exceptionsrun().catch(console.dir);运行上述示例将产生以下输出:
Successfully deleted one document.如果多次运行该示例,则代码会生成以下输出,因为您在第一次运行中删除了匹配的文档:
No documents matched the query. Deleted 0 documents.deleteMany() 示例:完整文件
注意
设置示例
该示例使用连接 URI 连接到 MongoDB 实例。如需了解有关连接到 MongoDB 实例的更多信息,请参阅连接到 MongoDB 指南。本示例还使用了Atlas 示例数据集中包含的 sample_mflix 数据库中的 movies 集合。您可以按照 MongoDB 入门指南,将它们加载到 MongoDB Atlas 的免费集群数据库中。
以下代码是一个完整的独立运行文件,用于执行删除多个操作:
// Delete multiple documents
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() { try { const database = client.db("sample_mflix"); const movies = database.collection("movies");
/* Delete all documents that match the specified regular expression in the title field from the "movies" collection */ const query = { title: { $regex: "Santa" } }; const result = await movies.deleteMany(query);
// Print the number of deleted documents console.log("Deleted " + result.deletedCount + " documents"); } finally { // Close the connection after the operation completes await client.close(); }}// Run the program and print any thrown exceptionsrun().catch(console.dir);// Delete multiple documents
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection stringconst uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() { try { const database = client.db("sample_mflix"); const movies = database.collection("movies");
/* Delete all documents that match the specified regular expression in the title field from the "movies" collection */ const result = await movies.deleteMany({ title: { $regex: "Santa" } }); // Print the number of deleted documents console.log("Deleted " + result.deletedCount + " documents"); } finally { // Close the connection after the operation completes await client.close(); }}// Run the program and print any thrown exceptionsrun().catch(console.dir);首次运行上述示例会产生以下输出:
Deleted 19 documents如果多次运行该示例,您将看到以下输出,因为您在第一次运行时删除了匹配的文档:
Deleted 0 documentsAPI 文档
要进一步了解本指南所讨论的任何类型或方法,请参阅以下 API 文档: