Stack operations for array in Mongo DB

By using $push, $pop, $pull, $pullAll, $addToSet we can make stack operations in the array of the document. we can add,delete, add multiple,add if not exists values into the array.

Initial data:
       {  "_id” : 3,  "Price" : [  20, 30, 40, 50  ]  }


db.prices.update({_id:3},{$push:{Price:60}})

The above Query will insert the 60 as the last element of the’ Price’ array
{ "_id" : 3,  "Price" : [  20, 30, 40, 50, 60  ]  }



db.prices.update({_id:3},{$pop:{Price:1}})

The above Query deletes the last element from in the ‘Price’ array
{ "_id" : 3,  "Price" : [  20, 30, 40, 50  ]  }


db.prices.update({_id:3},{$pop:{Price:-1}})

The above code will delete the records from the beginning of the ‘Price’ array
{ "_id" : 3,  "Price" : [  30, 40, 50  ]  }



db.prices.update({_id:3},{$pushAll:{Price:[70,80]}})

The above code will insert more than one record into ‘Price’ array at the end
{ "_id" : 3,  "Price" : [ 30, 40, 50, 70, 80 ]  }



db.prices.update({_id:3},{$pull:{Price:40}})

The above code removes the value ‘40’ from the ‘Price’ array
{ "_id" : 3,  "Price" : [  30, 50, 70, 80 ]  }



db.prices.update({_id:3},{$pullAll:{Price:[50,70]}})

The above Query removes the value 50 and 70 from the Price array
{ "_id" : 3, "Price" : [ 30, 80 ]  }



db.prices.update({_id:3},{$addToSet:{Price:100}})

The above Query will add the data to ‘Price’ array if the particular values don’t exists. If tha value already exists it will not add it back again and will not throw any error.
{ "_id" : 3,  "Price" : [  30, 80, 100 ]  }