
Already we have seen that the Field Update Operators in previous blog.
In this blog we can seen about Array and Bitwise Operators and it’s explanation.
Array Update Operators :
1.) $ :
To update the first element that matches the query condition.
Syntax :
{ "<array.$>" : value }
Explanation :
- The Array field must appear as part of the query document.
2.) $ [ ] :
To update all elements in an array for the document that matches the query condition.
Syntax :
{ < update operator > : { "<array>.$[]" : value } }
Explanation :
- The query must include an exact equality match on the array field in order to use the $[ ] positional operator in the update statement.
3.) $ [ <identifier> ] :
To update all elements that match the array filter condition for the document that matches the query condition.
Syntax :
{ < update operator > : { " <array>.$[ identifier ] " : value } } , { arrayFilters : [ { <identifier> : <condition> } ] }
Explanation :
- The <identifier> must begin with lowercase letter and contain only alphanumeric characters.
4.) $addToSet :
Adds elements to an array only if they don’t already exist in the set.
Syntax :
{ $addToSet : { <field1> : <value> , ..} }
Explanation :
- $addToSet only ensures that there are no duplicates items added to the set and does not affect existing duplicate elements.
- $addToSet does not guarantee a particular ordering of elements in the modified set.
5.) $pop :
Removes the first or last item of an array.
Syntax :
{ $pop : { <field> : < -1 | 1 > , ...} }
Explanation :
- The $pop operation fails if the <field> is not an array.
- If the $pop operator removes the last item in the <field>, the <field> will then hold an empty array.
6.) $pull :
Removes all elements that match a specified query.
Syntax :
{ $pull : { <field1> : < value | condition > , <field2> : < value | condition > , ... } }
Explanation :
- If the specified <value> to remove is an array, $pull removes only the elements in the array that match the specified <value> exactly,including order.
- If the specified <value> to remove is a document, $pull removes only the elements in the array that have the exact same fields and values, the ordering of the field can differ.
7.) $push :
Adds an item to an array.
Syntax :
{ $push : { <field> : <value> , ... } }
Explanation :
- The $push operator appends a specified value to an array.
- If the field is absent in the document to update, $push adds the array field with the value as its element.
- If the field is not an array, the operation will fail.
8.) $pullAll :
Removes all matching values from an array.
Syntax :
{ $pullAll : { <field1> : [ <value1> , <value2> , ... ] , ... } }
Explanation :
- If a <value> to remove is a document or an array, $pullAll removes only the elements in the array that match the specified <value> exactly,including order.
9.) $each :
Modifies the $push and $addToSet operators to append multiple items for array updates.
Syntax :
{ $addToSet : { <field> : { $each : [ <value1> , <value2> ... ] } } }
{ $push : { <field> : { $each : [ <value1> , <value2> ... ] } } }
Explanation :
- Use with the $addToSet operator to add multiple values to an array <field> if the values do not exist in the <field>.
- Use with the $push operator to append multiple values to an array <field>.
10.) $position :
Modified the $push operator to specify the position in the array to add elements.
Syntax :
{ $push : { <field> : { $each : [ <value1> , <value2> , ... ] , $position : <num> } } }
Explanation :
- The $position modifier specifies the location in the array at which the $push operator inserts elements.
- Without the $position modifier, the $push operator inserts elements to the end of the array.
11.) $slice :
Modifies the $push operator to limit the size of updated arrays.
Syntax :
{ $push : { <field> : { $each : [ <value1> , <value2> , ... ] , $slice : <num> } } }
Explanation :
- Use the $slice modifier, it must appear with the $each modifier, we can pass an empty array [ ] to the $each modifier such that only the $slice modifier has an effect.
12.) $sort :
Modifies the $push operator to reorder documents stored in an array.
Syntax :
{ $push : { <field> : { $each : [ <value1> , <value2> , ... ] , $sort : <sort specification> } } }
Explanation :
- The $sort modifier orders the elements of an array during a $push operation.
- To sort array elements that are not documents, or if the array elements are documents, to sort by the whole documents, specify 1 for ascending or -1 for descending.
Bitwise Update Operator :
1.) $bit :
It performs bitwise AND,OR and XOR updates of integer values.
Syntax :
{ $bit : { <field> : { <and | or | xor > : <int> } } }
Explanation :
- Only use this operator with integer fields ( either 32-bit integer or 64-bit integer ).
- The $bit operator performs a bitwise update of a field.
In this blog I have covered remaining update operators.