update Operators in MongoDB
What are update Operators in MongoDB?
The update() method updates the values in the existing document in MongoDB collections. When you update your document, the value of the _id field remains unchanged.
The update() method updates the values in the existing document in MongoDB collections. 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 and multi documents.
Parameters:
- The first parameter is the older value in the form of Documents. Documents are structures created of file and value pairs, similar to JSON objects.
- The second parameter must contain a $set keyword to update the following specific 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. The update method updates all the documents that meet the query condition when true; 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 set the language-specific rules for string comparison, like rules for lettercase and accent marks. The type of this parameter is a document.
- arrayFilters: An array of filter documents indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.
- Hint: A document or field specifies the index to support the filter. It can take an index specification document or the index name string, and if you select an index that does not exist, it will give an error.
- MongoDB provides different types of field update operators to update the values of the fields of the documents that match the specified condition.
The following table contains the field update operators:
Operator | Description |
$currentDate | This operator is used to set the value of a field to the current date, either as a Date or a Timestamp. |
$inc | This operator is used to increment the field's value by the specified amount. |
$min | This operator is used only to update the field if the specified value is less than the existing field value |
$max | This operator is used only to update the field if the specified value is greater than the current field value. |
$mul | This operator is used to multiply the field's value by the specified amount. |
$rename | This operator is used to rename a field. |
$setOnInsert | This operator is used to set the value of a field if an update results in an insert of a document. It does not affect update operations that modify existing documents. |