Update Operators of MongoDB


Update Operators are very useful while modifying the collections means while using db.collection.update(), db.collection.findAndModify().
The general way of usage of update operators are in this way

{
$operator: {<field1>:<value1>,<field2>:<value2>,…},
$operator: {<field1>:<value1>,<field2>:<value2>,…}
}

Some Key Update Operators

NameDescription
$incIncrements the value of the field by the specified amount.
$mulMultiplies the value of the field by the specified amount.
$renameRenames a field.
$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.
$setSets the value of a field in a document.
$unsetRemoves the specified field from a document.
$minOnly updates the field if the specified value is less than the existing field value.
$maxOnly updates the field if the specified value is greater than the existing field value.
$currentDateSets the value of a field to current date, either as a Date or a Timestamp.