Update Multiple Documents更新多个文档

Note

If you specify a callback method, updateMany() returns nothing. 如果指定回调方法,updateMany()将不返回任何内容。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 see the API documentation for information on the result object.有关更多信息,请参阅我们的承诺和回调指南,或参阅API文档以了解有关结果对象的信息。

You can update multiple documents using the collection.updateMany() method. 可以使用collection.updateMany()方法更新多个文档。The updateMany() method accepts a filter document and an update document. updateMany()方法接受筛选文档和更新文档。If the query matches documents in the collection, the method applies the updates from the update document to fields and values of the matching documents. 如果查询匹配集合中的文档,则该方法将更新文档中的更新应用于匹配文档的字段和值。The update document requires an update operator to modify a field in a document.更新文档要求更新运算符修改文档中的字段。

You can specify additional options in the options object passed in the third parameter of the updateMany() method. 可以在updateMany()方法的第三个参数中传递的options对象中指定其他选项。For more detailed information, see the updateMany() API documentation.有关更多详细信息,请参阅updateMany()API文档

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");
// create a filter to update all movies with a 'G' rating创建筛选器以更新所有分级为“G”的电影 const filter = { rated: "G" };
// increment every document matching the filter with 2 more comments将与筛选器匹配的每个文档增加2条注释 const updateDoc = { $set: { random_review: `After viewing I am ${ 100 * Math.random() }% more satisfied with life.`, }, }; const result = await movies.updateMany(filter, updateDoc); console.log(`Updated ${result.modifiedCount} 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);
enum Rating { G = "G", PG = "PG", PG_13 = "PG-13", R = "R", NR = "NOT RATED", }
interface Movie { rated: Rating; random_review?: string; }
async function run() { try { await client.connect();
const database = client.db("sample_mflix"); const movies = database.collection<Movie>("movies"); const result = await movies.updateMany( { rated: Rating.G }, { $set: { random_review: `After viewing I am ${ 100 * Math.random() }% more satisfied with life.`, }, } ); console.log(`Updated ${result.modifiedCount} documents`); } finally { await client.close(); } } run().catch(console.dir);

If you run the preceding example, you should see the following output:如果运行前面的示例,您应该会看到以下输出:

Updated 477 documents