Delete Multiple Documents删除多个文档

Note

If you specify a callback method, deleteMany() returns nothing. 如果指定回调方法,deleteMany()将不返回任何内容。If you do not specify one, this method returns a Promise that resolves to the result object when it completes. 如果未指定,此方法将返回一个Promise,该Promise在完成时解析为结果对象。See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.有关更多信息,请参阅我们的承诺和回调指南,或参阅API文档以了解有关结果对象的信息。

You can delete several documents in a collection at once using the collection.deleteMany() method. 可以使用collection.deleteMany()方法一次删除集合中的多个文档。Pass a query document to the deleteMany() method to specify a subset of documents in the collection to delete. 将查询文档传递给deleteMany()方法,以指定集合中要删除的文档子集。If you do not provide a query document (or if you provide an empty document), MongoDB matches all documents in the collection and deletes them. 如果不提供查询文档(或提供空文档),MongoDB将匹配集合中的所有文档并将其删除。While you can use deleteMany() to delete all documents in a collection, consider using drop() instead for better performance and clearer code.虽然可以使用DeleTimeYe()删除集合中的所有文档,但是考虑使用drop(),而不是更好的性能和更清晰的代码。

You can specify additional options in the options object passed in the second parameter of the deleteMany() method. 您可以在deleteMany()方法的第二个参数中传递的options对象中指定其他选项。You can also pass a callback method as the optional third parameter. 还可以将回调方法作为可选的第三个参数传递。For more detailed information, see the deleteMany() API documentation.有关更多详细信息,请参阅deleteMany()API文档

The following snippet deletes multiple documents from the movies collection. 以下代码段从movies集合中删除多个文档。It uses a query document that configures the query to match and delete movies with the title "Santa Claus".它使用一个查询文档,将查询配置为匹配和删除标题为“圣诞老人”的电影。

Note

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. 您可以使用此示例连接到MongoDB实例,并与包含示例数据的数据库交互。To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.要了解有关连接到MongoDB实例并加载示例数据集的更多信息,请参阅用法示例指南

import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。 const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() { try { await client.connect();
const database = client.db("sample_mflix"); const movies = database.collection("movies"); // Query for all movies with a title containing the string "Santa" const query = { title: { $regex: "Santa" } };
const result = await movies.deleteMany(query); console.log("Deleted " + result.deletedCount + " documents"); } finally { await client.close(); } } run().catch(console.dir);
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.用MongoDB部署的连接字符串替换uri字符串。 const uri = "<connection string uri>";
const client = new MongoClient(uri);
interface Movie { title: string; }
async function run() { try { await client.connect();
const database = client.db("sample_mflix"); const movies = database.collection<Movie>("movies"); const result = await movies.deleteMany({ title: { $regex: "Santa" } }); console.log("Deleted " + result.deletedCount + " documents"); } finally { await client.close(); } } run().catch(console.dir);

The first time you run the preceding example, you should see the following output:第一次运行上述示例时,应看到以下输出:

Deleted 19 documents

On subsequent runs of the example, as you already deleted all relevant documents, you should see the following output:在该示例的后续运行中,由于您已经删除了所有相关文档,您应该会看到以下输出:

Deleted 0 documents