Introduction to MongoDB collection.updateOne() Method

The updateOne() method is one of the methods in MongoDB used to update a single document. It can find the first document that matches the specified query condition and update it. This article will introduce the usage of the updateOne() method in four aspects: syntax, use cases, examples, and conclusions.

Syntax

The syntax of the updateOne() method is as follows:

db.collection.updateOne(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2
   }
)

Where the parameters are:

  • <filter>: the query condition used to specify the document to be updated.
  • <update>: the update operation used to specify the content to be updated.
  • upsert: an optional parameter that determines whether to insert a new document when there is no document that matches the query condition. The default value is false.
  • writeConcern: an optional parameter that sets the level of security for the write operation.
  • collation: an optional parameter used to specify the character set used in queries and sorting.
  • arrayFilters: an optional parameter used to update matching elements in an array.
  • hint: an optional parameter used to specify the index used in the query.

Use Cases

The updateOne() method is suitable for scenarios where a single document needs to be updated, such as modifying a user’s personal information, updating the status of a task, etc. It can perform document update operations based on specific conditions, supports multiple update operators and options, and can flexibly modify data.

Examples

We will introduce the usage of the updateOne() method through two examples.

  1. Modify the password of a user

    Suppose we need to modify the password of a user named “Alice” to “newpassword”, we can use the following code:

    db.users.updateOne({ name: "Alice" }, { $set: { password: "newpassword" } })

    The code will find the first document in the users collection that matches the condition { name: "Alice" }, and modify the password field to “newpassword”.

  2. Update the status and price of an order

    Suppose we have an orders collection, where each document represents an order and contains the following fields:

    {
      _id: ObjectId("1234567890abcdef"),
      status: "open",
      price: 100
    }
    

    Now, we need to modify the status of the order with the order number “1234567890abcdef” to “closed” and the price to 120. We can use the following code:

    db.orders.updateOne(
      { _id: ObjectId("1234567890abcdef") },
      { $set: { status: "closed", price: 120 } }
    )
    

    The above code will find the document in the orders collection where the _id field is “1234567890abcdef” and modify the status field to “closed” and the price field to 120.

Conclusion

updateOne() is one of the methods in MongoDB used to update a single document. It can query the first document that meets the specified condition and update it accordingly.