How to Insert, Update, Delete Elements in MongoDB

here are the kive examples so that you can easily understand the query execution in MongoDB


find using filters


As an array


find using greater than the value

/* $gt is use for grater and $lt is use for less than */

delete data from document deleteOne


same idea logy for multiple deletions  deleteMany


for update we use updateMany, updateOne, replaceOne, update(not recommend)

Now we will see how to update age which was 29 to something else


now check

replaceOne, and update use set of object data and not use $set 

simply update will replace the data having data to change . Let us see an example with object ID of Amit panchal above


here we can see index name,age removed totally from json. A complete new object data store . This will override our data in database.
Hence we use replaceOne method 

db.customers.replaceOne({name1:"Hi I am Amit PAnchal"},{name:"Amit Panchal",age:22}) 

and view 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codeforbetter will use the information you provide on this form to be in touch with you and to provide updates and marketing.