MongoDB update()

The MongoDB update() method is a method that is used to update a single document or multiple documents in the collection. When the document is updated the _id field remains unchanged.

The db.collection.update() method updates a single document by default. To update all documents that match the given query, use the multi: true option. Include the option “multi: true“.

Note: In MongoDB, the update() method has been deprecated in favor of using updateOne() or updateMany() depending on your specific use case.

Syntax

db.COLLECTION_NAME.update({SELECTION_CRITERIA}, {$set:{UPDATED_DATA}}, {
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
})

Explanation: This MongoDB update operation updates documents that match the selection criteria with the specified updated data. Here’s an explanation of the parameters used:

  • COLLECTION_NAME: The name of the collection to update.
  • SELECTION_CRITERIA: The criteria to select which documents to update.
  • UPDATED_DATA: The new data to set for the selected documents.
  • upsert: (Optional) If set to true, creates a new document when no document matches the selection criteria.
  • multi: (Optional) If set to true, updates multiple documents that match the selection criteria. Default is false, which updates only the first matching document.
  • writeConcern: (Optional) Specifies the level of write concern for the operation.
  • collation: (Optional) Specifies the collation for string comparisons.
  • arrayFilters: (Optional) Filters to determine which elements to modify in an array field.
  • hint: (Optional) Forces MongoDB to use a specific index for the operation.

MongoDB – Update() Method

MongoDB update operations allow us to modify documents in a collection. These operations can update a single document or multiple documents based on specified criteria.

MongoDB offers various update operators to perform specific actions like setting a value, incrementing a value or updating elements within arrays. In this article, We will learn about MongoDB update() by understanding various examples in detail.

Similar Reads

MongoDB update()

The MongoDB update() method is a method that is used to update a single document or multiple documents in the collection. When the document is updated the _id field remains unchanged....

Examples of MongoDB update

Let set up an environment:...

Conclusion

Overall, MongoDB update operations provide a powerful way to modify documents in a collection. Whether you need to update a single document, multiple documents, or perform complex updates using aggregation pipelines, MongoDB’s update capabilities offer flexibility and efficiency. By mastering these update operations, you can efficiently manage your MongoDB data and tailor it to meet your application’s needs...

FAQs on MongoDB update()

What is the difference between updateOne() and updateMany() in MongoDB?...