MongoDB – Insert,Update,Upsert and Delete Examples – CRUD

In MongoDB we have to use either insert() or save() method to add the document to a collection

Insert Single document

> db.employee.insert( 
    "dob"new Date(1983,2,3,5,20), 
            'DB Amdin','DB Developer' 
    "Location":"New York" 

Insert Multiple document –

We have to use an array to pass multiple documents and its enclosed in a square brackets,separated by comma.

"employee_id":1102"name":"Jayaram""sal":95000"dob"new Date(1983,18,4,12,05), 
"department":"Web Admin""Location":"New York" 
"employee_id":1103"name":"Pravitha""sal":195000"dob"new Date(2015,07,06,11,22), 
"department":['Health Science','Scientist'], 
"employee_id":1104"name":"Prarthana""sal":295000"dob"new Date(2015,07,06,11,23), 
"employee_id":1105"name":"Ambika""sal":80000"dob"new Date(1983,2,3,5,20), 
"department":['DB Amdin','DB Developer'], 

Mongo DB’s Update

Mongo DB’s update() method used to update values of an existing document

  • Update the document Prashan and set the sal to 100K
>db.employee.update({name: 'Prashan'},{$set: {sal: 100000}})
  •   Update sal to 10000 for all. To update multiple document you need to set a parameter multi to true.

>db.employee.update({},{$set: {sal:10000}},{multi:true})

Use of Multiply operator in Update

  • multiply the sal by 10 where sal is > 10000 for all
>db.employee.update({sal:{$gt:10000}},{$mul: {sal:10}},{multi:true})



The Users document

The users collection has four documents and its details are as follows



"name" : "Prayer",
"age" : 1,
"status" : "B"
"name" : "Pravitha",
"age" : 10,
"status" : "C"
"name" : "Prashanth",
"age" : 33,
"status" : "A"
"name" : "Ambika",
"age" : 32,
"status" : "E"
 In the below example, the status is update to “N” where age>18 of the users collection
>db.users.update({age:{$gt:18}},{$set: {status:"N"}},{multi:true})

userUpdateUse Multiple criteria’s to update the document

  • In the below example, the status is updated to “New” where age<15 and Name is Pravitha of the users collection
>db.users.update( {"age": {$lt :15}, "name" : "Pravitha" }, { $set : { status: "New" } } )

Use Regular Expression

  • In the below example, the search parameter ‘Pra’ is searched and updated its status to “Yes”
>var search='Pra'
>db.users.find({name : new RegExp(search)}).forEach(function(doc) { db.users.update({_id:doc._id},{$set:{"status":"Yes"}})})


MongoDB’s Upserts

• Upserts are a special type of inserts that allows a document to be inserted if it is not found by the update criteria
• We specify upsert: true inside the db.collection.update function

>db.employee.update({name: 'Prayer'},{$set: {sal: 100000}},{upsert:true})

MongoDB’s Remove

MongoDB’s remove() method is used to remove document from the collection

• To delete a document from the database, we can use db.collection.remove() function.
• To remove all documents, we can use – db.collection.remove({})

Remove a document


Remove a document based on condition


Remove All documents


About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication and PowerShell You can connect me via jayaram/ My Articles are published in following sites
This entry was posted in MongoDB, Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s