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( 
    { 
    "employee_id":1101, 
    "name":"Prashanth", 
    "sal":90000, 
    "dob"new Date(1983,2,3,5,20), 
    "department": 
        [ 
            '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.

>db.employee.save( 
[ 
{ 
"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'], 
"Location":"NJ" 
}{ 
"employee_id":1104"name":"Prarthana""sal":295000"dob"new Date(2015,07,06,11,23), 
"department":['Engineer','Pilot'], 
"Location":"NJ" 
}, 
{
"employee_id":1105"name":"Ambika""sal":80000"dob"new Date(1983,2,3,5,20), 
"department":['DB Amdin','DB Developer'], 
"Location":"Dallas" 
}, 
] 
);

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})

EmployeesalaryUpdate

 

The Users document

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

 

>db.users.insert(

[{
"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"}})})

 UsersUpdate.jpg

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

>db.employee.remove({"name":"Prashanth"})

Remove a document based on condition

>db.employee.remove({"sal":{$lt:85000}})

Remove All documents

>db.employee.remove({})
Advertisements

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 https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ https://www.tumblr.com/blog/prashantjayaram http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ http://www.toadworld.com/members/prashanthjayaram/ My Articles are published in following sites http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
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:

WordPress.com Logo

You are commenting using your WordPress.com 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