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

Update a Document

You can update a single document using the collection.updateOne() method. The updateOne() 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 them. The update document contains update operators that instruct the method on the changes to make to the matches.

You can specify additional query options using the options object passed as the second parameter of the updateOne() method. Set the upsert option to true to create a new document if no documents match the filter. For additional information, see the updateOne() API documentation.

updateOne() throws an exception if an error occurs during execution. If you specify a value in your update document for the immutable field _id, the method throws an exception. If your update document contains a value that violates unique index rules, the method throws a duplicate key error exception.

Note

If your application requires the document after updating, consider using the collection.findOneAndUpdate(). method, which has a similar interface to updateOne() but also returns the original or updated document.

The following example uses the $set update operator which specifies update values for document fields. For more information on update operators, see the MongoDB update operator reference 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.

1import { MongoClient } from "mongodb";
2
3// Replace the uri string with your MongoDB deployment's connection string.
4const uri = "<connection string uri>";
5
6const client = new MongoClient(uri);
7
8async function run() {
9 try {
10 const database = client.db("sample_mflix");
11 const movies = database.collection("movies");
12
13 // create a filter for a movie to update
14 const filter = { title: "Random Harvest" };
15
16 // this option instructs the method to create a document if no documents match the filter
17 const options = { upsert: true };
18
19 // create a document that sets the plot of the movie
20 const updateDoc = {
21 $set: {
22 plot: `A harvest of random numbers, such as: ${Math.random()}`
23 },
24 };
25
26 const result = await movies.updateOne(filter, updateDoc, options);
27 console.log(
28 `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
29 );
30 } finally {
31 await client.close();
32 }
33}
34run().catch(console.dir);
1import { MongoClient } from "mongodb";
2
3// Replace the uri string with your MongoDB deployment's connection string.
4const uri = "<connection string uri>";
5
6const client = new MongoClient(uri);
7
8interface Movie {
9 plot: string;
10 title: string;
11}
12
13async function run() {
14 try {
15 const database = client.db("sample_mflix");
16 const movies = database.collection<Movie>("movies");
17
18 const result = await movies.updateOne(
19 { title: "Random Harvest" },
20 {
21 $set: {
22 plot: `A harvest of random numbers, such as: ${Math.random()}`,
23 },
24 },
25 { upsert: true }
26 );
27 console.log(
28 `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
29 );
30 } finally {
31 await client.close();
32 }
33}
34run().catch(console.dir);

If you run the example above, you should see the following output:

1 document(s) matched the filter, updated 1 document(s)

Back

Update & Replace Operations