MongoDB – Update() Method

22 Oct, 2020
The update() method updates the values in the existing document in the collections of MongoDB. When you update your document the value of the _id field remains unchanged. By default, the db.collection.update() method updates a single document. Include the option multi: true to update all documents that match the given query. This method can be used for a single updating of documents as well as multi documents.
Syntax:
db.COLLECTION_NAME.update({SELECTION_CRITERIA}, {$set:{UPDATED_DATA}}, {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        
   })


Parameters:
The first parameter is the Older value in the form of Documents. Documents are a structure created of file and value pairs, similar to JSON objects.
The second parameter must contain a $set keyword to update the following specify document value.
The third parameter is optional.
Optional Parameters:
Upsert: The default value of this parameter is false. When it is true it will make a new document in the collection when no document matches the given condition in the update method.
multi: The default value of this parameter is false. When it is true the update method update all the documents that meet the query condition. Otherwise, it will update only one document.
writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is a document.
Collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is a document.
arrayFilters: It is an array of filter documents that indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.
hint: It is a document or field that specifies the index to use to support the filter. It can take an index specification document or the index name string and if you specify an index that does not exist, then it will give an error.
Examples:
In the following examples, we are working with:
Database: gfg
Collections: student
Document: Three documents contains name and the age of the students
Update the name of the document whose name key has avi value to hello world.
db.student.update({name:"avi"},{$set:{name:"helloword"}})

Here, the first parameter is the document whose value to be changed {name:”avi”} and the second parameter is set keyword means to set(update) the following matched key value with the older key value.
Note: The value of the key must be of the same datatype that was defined in the collection.
Update the age of the document whose name is prachi to 20.
db.student.update({name:"prachi"},{$set:{age:20}}

Here, the first parameter is the document whose value to be changed {name:”prachi”} and the second parameter is set keyword means to set(update) the value of the age field to 20.
MongoDB
MongoDB-method
MongoDB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to connect MongoDB with ReactJS ?
MongoDB - limit() Method
MongoDB - sort() Method
MongoDB - FindOne() Method
MongoDB updateOne() Method - db.Collection.updateOne()
MongoDB - Regex
MongoDB - Compound Indexes
MongoDB updateMany() Method - db.Collection.updateMany()
MongoDB Cursor
Mongoose | update() Function