MongoDB v4.2 Update Operators – Part I

In this blog we can seen what are update operators were present in MongoDB and it’s explanation. 

  • Field Update Operators.
  • Array Update Operators.
  • Bitwise Update Operator. 

Modifier are available for use in update operations eg.

db.collection.update()

db.collection.findAndModfiy()

Field Update Operators :

1. ) $currentDate :

Set the value of a field to the current date.

Syntax :

{ $currentDate : { <field1> : <typeSpecification1>, ...} }

Explanation :

  • If the field does not exist, $currentDate adds the field to a document.

2. ) $inc :

Increment the value of field by specified amount.

Syntax :

{ $inc : { <field1> : <amount1>, <field2> : <amount2>, ... } }

Explanation :

  • It accepts positive and negative values.
  • Use of the $inc operator on a field with a null value will generate an error.
  • $inc is an atomic operation with a single document.

3. ) $min :

Only updates the field if the specified value is less than existing field value.

Syntax :

{ $min : { <field1> : <value1>, ...}  }

Explanation : 
  • For comparisons between value of different types,such as a number and a null.
  • $min uses the BSON comparison order.

4. ) $max :

Only updates the field if the specified value is greater than existing field value.

Syntax :

{ $max : { <field1> : <value1>, ...}  }

Explanation :

  • $max uses the BSON comparison order.
  • If the field doesn’t exist,the $max operator sets the field to the specified value.

5. ) $mul :

Multiplies the value of the field by the specified amount.

Syntax :

{ $mul : { <field1> : <number1>, ...} }

Explanation :

  • If the field does not exist in a document,$mul creates the field and sets the value to zero of the same numeric type as the multiplier.

6. ) $rename :

Renames a field.

Syntax :

{ $rename : { <field1> : <newName1>, ...} }

Explanation :

  • The $rename operator logically performs an $unset both the old name and the new name, and then perform $set operation with the new name i.e) the renamed field may move within the document.
  • If the field to rename does not exist in a document,$rename does nothing i.e) no operation.

7. ) $set :

Sets the value of a field in a document.

Syntax :

{ $set : { <field1> : <value1>, ...} }

Explanation :

  • If the field doesn’t exist,the $set operator will add a new field with the specified value and provided that the new field does not violate a type constraint.
  • If you specify a dotted path for a non-existent field,$set will create the embedded documents as needed to fulfill the dotted path to the field.

8. ) $setOnInsert :

Update results in an insert of a document.

Syntax :

{ $setOnInsert : { <field1>: <value1>, ... } } , { upsert : true }

Explanation :

  • If an update operation with upsert : true results in an insert of a document, then $setOnInsert assigns the specified values to the fields in the document.
  • If the update operation does not result in an insert,$setOnInsert does nothing.

9. ) $unset :

Remove the specified field from a document.

Syntax :

{ $unset : { <field1> : "", ... } }

Explanation :

  • If the field does not exist, then $unset does nothing i.e) no operation.
  • When used with $ to match an array element, then $unset replaces the matching element will null rather than removing the matching element from the array.

In this blog I have covered Field Update Operators, Later we will see remaining Update Operators.

Published by arunkumarblog

RHCE Linux RedHat Certified Professionals,Working on MongoDB and MySQL Database Administrator.

Leave a comment

Design a site like this with WordPress.com
Get started