Update a Document

Info With Circle IconCreated with Sketch.Note

If you specify a callback method, updateOne() returns nothing. If you 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 the API documentation for information on the result object.

You can update a single document using the collection.updateOne() method. updateOne() 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.

Info With Circle IconCreated with Sketch.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.

Info With Circle IconCreated with Sketch.Note

This example connects to an instance of MongoDB using a connection string. To learn more about connecting to your MongoDB instance, see the connection guide.

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