MongoDB Update OperatorThe 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$currentDateIt 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: $incIt increases a filed by the specified value. Syntax: Example: $minIt 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: $maxIt 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: $mulIt multiplies the value of a field by a number. Syntax: Example: $renameThe rename operator changes the name of a field. Syntax: Example: $setThe set operator changes the value of a field with the specified value. Syntax: Example: $setOnInsertIf 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: $unsetIt 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: $addToSetIt adds an element to an array unless the element is already present, in which case this operator does nothing to that array. Syntax: Example: $popWe 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: $pullUsing a pull operator, we can remove all the instances of a value in an array that matches the specified condition. Syntax: Example: $pushIt appends a specified value to an array. Syntax: Example: $pullAllWe 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$eachIt 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: $positionIt specifies the location where the push operator inserts elements inside an array. Syntax: Example: $sliceThis modifier is used to limit the number of array elements during the push operation. Syntax: Example: $sortThe sort modifier arranges the values of an array during the push operation. Syntax: Example: Bitwise Operator$bitThe bit operator updates a field using a bitwise operation. It supports bitwise AND, bitwise OR, and bitwise XOR operations. Syntax: Example: Next TopicMongoDB |