Docs Menu
Docs Home
/ / /
Node.js Driver
/ /

Update Multiple Documents

You can update multiple documents using the collection.updateMany() method. The updateMany() method accepts a filter document and an update document. 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 more options in the options object passed in the third parameter of the updateMany() method. For more detailed information, see the updateMany() API documentation.

Note

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.

1/* Update 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 // Get the "movies" collection in the "sample_mflix" database
13 const database = client.db("sample_mflix");
14 const movies = database.collection("movies");
15
16 // Create a filter to update all movies with a 'G' rating
17 const filter = { rated: "G" };
18
19 // Create an update document specifying the change to make
20 const updateDoc = {
21 $set: {
22 random_review: `After viewing I am ${
23 100 * Math.random()
24 }% more satisfied with life.`,
25 },
26 };
27 // Update the documents that match the specified filter
28 const result = await movies.updateMany(filter, updateDoc);
29 console.log(`Updated ${result.modifiedCount} documents`);
30 } finally {
31 // Close the database connection on completion or error
32 await client.close();
33 }
34}
35run().catch(console.dir);
1/* Update 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
10enum Rating {
11 G = "G",
12 PG = "PG",
13 PG_13 = "PG-13",
14 R = "R",
15 NR = "NOT RATED",
16}
17
18// Create a Movie interface
19interface Movie {
20 rated: Rating;
21 random_review?: string;
22}
23
24async function run() {
25 try {
26 // Get the "movies" collection in the "sample_mflix" database
27 const database = client.db("sample_mflix");
28 const movies = database.collection<Movie>("movies");
29
30 // Update all documents that match the specified filter
31 const result = await movies.updateMany(
32 { rated: Rating.G },
33 {
34 $set: {
35 random_review: `After viewing I am ${
36 100 * Math.random()
37 }% more satisfied with life.`,
38 },
39 }
40 );
41 console.log(`Updated ${result.modifiedCount} documents`);
42 } finally {
43 // Close the database connection on completion or error
44 await client.close();
45 }
46}
47run().catch(console.dir);

Running the preceding example, you see the following output:

Updated 477 documents

Back

Update a Document

Next

Replace a Document