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 ] }
2 Comments
ReplyDeleteNever seen article like this, thanks for the wonderful content.
SEO Training in Chennai
JAVA Training in Chennai
Big Data Training in Chennai
Selenium Training in Chennai
German Classes in chennai
DOT NET Training in Chennai
Salesforce Training in Chennai
Salesforce Training in Anna Nagar
IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
ReplyDeleteJavaScript Online Training in India
JavaScript Training in India
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training