Introduction to Mongo DB updating Document Part2

Rama Sagar
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 4282 red flag
Rating: 5 out of 5  
 1 vote(s)

MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
Read Introduction to Mongo DB updating Document before this article.


In this article we will learn how to update document in mongo dB with a practical example.

Previous articles covered an introduction to mongo dB and the set up and installation, and few command line options.You can access those articles from here:


The objective of the article is to learn about updating document in Mongo database.

Updating Document

In previous article we have seen how to update a document using update command here we will see some more options of updating a document.

Now lets consider some array operations

We can see below that I have a document with only an id field ({_id:1});

Now if we want here an array containing some values in this document

We can issue the update with the push command to add an item to an array

db.a.update ({_id:1}, {$push :{ things: ’one’}});

Lets put some more items

db.a.update ({_id:1}, {$push :{ things: ‘two’}});

db.a.update ({_id:1}, {$push :{ things: ‘three’}});

db.a.update ({_id:1}, {$push :{ things: ‘three’}});


We can find that it has the element three twice that might be something we don’t want.. We can restrict it by using addToSet operator

db.a.update ({_id:1}, {$addToSet :{ things: ‘four’}});

We can notice that the element four didn't added again

We can still see three is hanging in which  we doesnt need we can remove it out of the array by using pull operator and can add it again as shown..

db.a.update ({_id:1}, {$pull :{ things: ‘three}});

It will delete all the instances of three in the array as shown below

Now if we want to remove the last element and first element in an array we can use pop operator as shown below

For last element

db.a.update ({_id:1}, {$pop :{ things: 1}});

For First element we can use negative value

db.a.update ({_id:1}, {$pop :{ things: -1}});

Now lets consider a scenario where we had multiple records in the database...if we wanna apply an update to several of them

Lets see if we wanna push another element in them using the update command with an empty query we can do it

db.a.update ({}, {$push :{ things: 4}});

But we can see that only one record was effected this is because the default options of an update is fixed to one record.

we can fix it by using the multiple options true

db.a.update ({}, {$push :{ things: 4}},{multi:true));


In this article we have seen different options in updating a document in mongo database.In coming articles we will see more about querying.


Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)