Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Update a Document

Note

If you specify a callback method, updateOne() returns nothing. If you do not specify one, this method returns a Promise that resolves to the result object when it completes. See our guide on Promises and Callbacks for more information, or see the API documentation for information on the result object.

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

This example connects to an instance of MongoDB and uses a sample data database. To learn more about connecting to your MongoDB instance and loading this database, see the Usage Examples guide.

1const { MongoClient } = require("mongodb");
2
3// Replace the uri string with your MongoDB deployment's connection string.
4const uri =
5 "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
6
7const client = new MongoClient(uri, {
8 useNewUrlParser: true,
9 useUnifiedTopology: true,
10});
11
12async function run() {
13 try {
14 await client.connect();
15
16 const database = client.db("sample_mflix");
17 const movies = database.collection("movies");
18
19 // create a filter for a movie to update
20 const filter = { title: "Blacksmith Scene" };
21
22 // this option instructs the method to create a document if no documents match the filter
23 const options = { upsert: true };
24
25 // create a document that sets the plot of the movie
26 const updateDoc = {
27 $set: {
28 plot:
29 "Blacksmith Scene is a silent film directed by William K.L. Dickson.",
30 },
31 };
32
33 const result = await movies.updateOne(filter, updateDoc, options);
34 console.log(
35 `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
36 );
37 } finally {
38 await client.close();
39 }
40}
41run().catch(console.dir);
←  Update & Replace OperationsUpdate Multiple Documents →