Introduction to MongoDB collection.update() Method

MongoDB is a popular NoSQL database that supports multiple query languages and methods. In MongoDB, the update() method is used to modify data in one or more documents.

The update() method is used to modify data in documents and can accept multiple parameters, including filtering conditions and data to be updated. When the update() method is executed, MongoDB will search for documents that match the filtering conditions and update them to the specified data.

Syntax

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

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Where:

  • query: Specifies the filtering conditions for the documents to be modified. Various operators (such as $eq, $ne, $gt, etc.) can be used to construct filtering conditions.
  • update: Specifies the data to update the documents to. Operators such as $set, $unset, and $inc can be used to update data.
  • upsert: An optional parameter that indicates whether to insert a new document if no document matches the filtering conditions. The default is false.
  • multi: An optional parameter that indicates whether to update all documents that match the filtering conditions at the same time if there are multiple documents. The default is false.
  • writeConcern: An optional parameter that indicates the security level when writing data.

Use Cases

The update() method is usually used to modify specific fields in documents or to add or delete fields. It can also be used to update documents that match the filtering conditions in batches.

Example 1: Modifying a Single Document

Assume that we have a collection named users, which contains the following documents:

{ "_id": 1, "name": "Alice", "age": 25 }
{ "_id": 2, "name": "Bob", "age": 35 }
{ "_id": 3, "name": "Charlie", "age": 40 }

Now, we want to change the age field of the document with _id 1 to 30. We can use the following update() method:

db.users.update({ _id: 1 }, { $set: { age: 30 } })

After execution, the users collection will become:

{ "_id": 1, "name": "Alice", "age": 30 }
{ "_id": 2, "name": "Bob", "age": 35 }
{ "_id": 3, "name": "Charlie", "age": 40 }

Example 2: Batch Updating Documents

Assume that we want to change the value of the status field of all documents with an age greater than 30 to "inactive". We can use the following update() method:

db.users.update(
  { age: { $gt: 30 } },
  { $set: { status: "inactive" } },
  { multi: true }
)

In this method, the first argument { age: { $gt: 30 } } is the query condition, used to match all documents with age greater than 30. The second argument { $set: { status: "inactive" } } is the update operation, used to set the value of the status field to “inactive”. The third argument { multi: true } indicates that we want to perform the update operation on all matched documents, not just the first one.

After executing the update() method, the value of the status field in all documents that meet the condition will be updated to “inactive”.

Conclusion

Through this article, we have learned about the update() method in MongoDB. This method can be used for updating a single document or for updating multiple documents that match the condition. When using this method, you need to specify the query condition and the update operation. Using the update() method makes it easy to modify documents in a MongoDB database.