MongoDB - How to Update Array fields as part of Documents in MongoDB using Robo 3T - II - TechDB

Latest

All about Database Programming, Performance Tuning and Best Practices.

BANNER 728X90

Tuesday, 21 January 2020

MongoDB - How to Update Array fields as part of Documents in MongoDB using Robo 3T - II



On continue from previous post lets see more options to update array.

$pushAdds an item to an array
If you want to add elements without checking (elements exist or not) use $push instead of $addtoset.
$push will add the given object to field whether it exists or not.
Example - 1 - Lets check how $push modifier works.
Below command I executed 2 times. And see the behaviour of $push.
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:["red","brown"]}}
   }
)
Lets check the output.
You can see elements “red” and “brown” added two times.
$position: 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
Syntax:
{
  $push: {
    <field>: {
       $each: [ <value1>, <value2>, ... ],
       $position: <num>
    }
  }
}
Example - 2: Add two elements at position 0, means at the beginning. By default / without $position elements will add in last.
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:["white1","white2"],
                        $position:0
                      }
            }
   }
)
Similarly
$position = 2 means new elements will add after color(2).
$position = -1 then new elemnets will add before color(5)
$slice  : - The $slice modifier limits the number of array elements during a $push operation. To project, or return, a specified number of array elements from a read operation, see the $slice projection operator instead.
Syntax:
{
  $push: {
     <field>: {
       $each: [ <value1>, <value2>, ... ],
       $slice: <num>
     }
  }
}
At initial we have only two elements of color array. In below examples we are trying to add elements with $slice modifier to validate the output.
Example - 3  - Slice from the End of the Array
The following operation adds new elements to the color array, and then uses the $slice modifier to trim the array to the last four elements:
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:["white","black","green"],$slice: -4}}
   }
)
Example - 4  - Slice from the Front of the Array
The following operation adds new elements to the scores array, and then uses the $slice modifier to trim the array to the first three elements.
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:["white","black","green"],$slice: 3}}
   }
)
Example - 5  - Update Array Using Slice Only
To update the scores field with just the effects of the $slice modifier, specify the number of elements to slice (e.g. 2) for the $slice modifier and an empty array [] for the $each modifier, as in the following:
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:[],$slice: 2}}
   }
)
$sort : -  The $sort modifier orders the elements of an array during a $push operation. To use the $sort modifier, it must appear with the $each modifier. You can pass an empty array [] to the $each modifier such that only the $sort modifier has an effect.
Syntax:
{
  $push: {
     <field>: {
       $each: [ <value1>, <value2>, ... ],
       $sort: <sort specification>
     }
  }
}
db.inventory.update(
   { item : "journal" },
   {
     $push: {"color": {$each:[],$sort: 1}}
   }
)
$pop: - The $pop operator removes the first or last element of an array. Pass $pop a value of -1 to remove the first element of an array and 1 to remove the last element in an array.
db.inventory.update(
   { item : "journal" },
   {
     $pop: {"color":-1 }
   }
)
$pull: - The $pull operator removes from an existing array all instances of a value or values that match a specified condition
Initially the color array has 3 elements.
Now we are trying to remove middle element “black” from array using $pull operator.
db.inventory.update(
   { item : "canvas" },
   {
     $pull: {"color":{$in:["black"]} }
   }
)
You can see color “black” removed from array.
Similarly you can remove elements from multiple array in one command, additionally have to use multi:true



No comments:

Post a Comment