Step-by-step Instructions on the MongoDB Update() Method

The update() method updates the value in an existing document in the MongoDB collection. When you update a document, the values of the _id fields remain the same. By default, db.collection. update() method to update a single document. Include the option multi:true to update all documents that match a given query. This method can be used for a single update of a document as well as for multiple documents.

The syntax is as follows:

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

Parameter:

  • The first parameter is the “older” value in the form of “document”. A document is a structure created by pairs of files and values, similar to a JSON object.
  • The second parameter must contain a $set keyword to update the document value specified below.
  • The third parameter is optional.

Optional parameters:

  • Upsert: The default value for this parameter is false. If true, it will create a new document in the collection when there are no documents in the update method that match the given criteria.
  • Many: The default value for this parameter is false. If true, the update method will update all documents that match the query criteria. Otherwise, it will update only one document.
  • writeConcern: Use it only if you don’t want to use the default write concern. The type of this parameter is document.
  • Collation: It specifies that operations are performed using collation. It allows users to specify language-specific rules for string comparisons, such as letter capitalization and accent mark rules. The type of this parameter is document.
  • arrayFilters: This is an array of filter documents that indicate which array elements to modify for update operations on the array field. The type of this parameter is array.
  • Implication: It’s a document or field that specifies the index used to support the filter. It can use an index canonical document or an index name string, and if you specify an index that doesn’t exist, it will give an error.

Example:

In the following example, we’re using:

Database: GFG Collection: Student Documents: Three documents contain the names and ages of the students

  • Documents with their name keyword having an avi value are updated to hello world.
db.student.update({name:"avi"}, {$set:{name:"helloword"}})

Here, the first parameter is the document whose value you want to change {name:”avi”}, and the second parameter is the set keyword which means to set (update) the following matching key values with the old key values.

Note: The value of the key must be the same as the data type defined in the collection.

  • Update the age of the document named Prachi to 20.
db.student.update({name:"prachi"}, {$set:{age:20}}

Here, the first parameter is the document whose value you want to change {name:”prachi”}, and the second parameter is the set keyword which means setting (updating) the value of the age field to 20.