Javatpoint Logo
Javatpoint Logo

MongoDB Update Operator

The following modifiers are available to update operations. For example - in db.collection.update() and db.collection.findAndModify().

Defines the operator expression in the document of the form:

Field Operator

$currentDate

It updates the elements of a field to the current date, either as a Date or a timestamp. The default data type of this operator is the date.

Syntax:

Example:

$inc

It increases a filed by the specified value.

Syntax:

Example:

$min

It changes the value of the field to a specified value if the specified value is less than the current value of the filed.

Syntax:

Example:

$max

It changes the value of the field to a specified value if the specified value is greater than the current value of the filed.

Syntax:

Example:

$mul

It multiplies the value of a field by a number.

Syntax:

Example:

$rename

The rename operator changes the name of a field.

Syntax:

Example:

$set

The set operator changes the value of a field with the specified value.

Syntax:

Example:

$setOnInsert

If the upsert is set to true, then it results in an insert of a document, then setOnInsert operator assigns the specified values to the field in the document.

Syntax:

$unset

It removes a specified field.

Syntax:

Example:

Array Operators

$

We can update an element in an array without explicitly specifying the position of the element.

Syntax:

Example:

$[ ]

The positional operator indicates that the update operator should change all the elements in the given array field.

Syntax:

Example:

$[<identifier>]

It is called a filtered positional operator that identifies the array elements.

Syntax:

Example:

$addToSet

It adds an element to an array unless the element is already present, in which case this operator does nothing to that array.

Syntax:

Example:

$pop

We can remove the first or last element of an array using the pop operator. We need to pass the value of pop as -1 to remove the first element of an array and 1 to remove the last element in an array.

Syntax:

Example:

$pull

Using a pull operator, we can remove all the instances of a value in an array that matches the specified condition.

Syntax:

Example:

$push

It appends a specified value to an array.

Syntax:

Example:

$pullAll

We can remove all instances of the specified value from an existing array using the pullAll operator. It removes elements that match the listed value.

Syntax:

Example:

Modifiers

$each

It is used with the $addToSet operator and the $push operator. It is used with the addToSet operator to add multiple values to an array if the value does not exist in the field.

Syntax:

It is used with the push operator to append multiple values to an array.

Syntax:

Example:

$position

It specifies the location where the push operator inserts elements inside an array.

Syntax:

Example:

$slice

This modifier is used to limit the number of array elements during the push operation.

Syntax:

Example:

$sort

The sort modifier arranges the values of an array during the push operation.

Syntax:

Example:

Bitwise Operator

$bit

The bit operator updates a field using a bitwise operation. It supports bitwise AND, bitwise OR, and bitwise XOR operations.

Syntax:

Example:


Next TopicMongoDB




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA