Introduction to Mongo DB updating Document

Rama Sagar
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 4983 red flag
Rating: 4.5 out of 5  
 2 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.
Recommendation
Read Introduction to Mongo db Storing Data Part2 before this article.

Introduction

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

previous articles have provided an introduction to mongo dB and the set up and installation, and few command line options.You can get them from the following:


Objective

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

Updating Document

The mongo update command is atomic within a document. No two clients may update the same document at the same time, two update commands issued concurrently will be executed one after another. Here is the syntax of the update command...

First we have to specify which collection we gonna update

Second we will need to specify which document we are targeting

Third we will need to specify what change we want to see in active i.e. update parameter

Lastly we may specify other options such as do we want to change only one the first document found matching the query or do we want to upsert(do we want to save a new record in case the query doesn’t match any  document will generate the document on the fly)


Let’s see them in a practical example

First let’s save a record with the id 1 and a value x of 10...

db.a.save({_id:1, X:10});

Here we use the increment operator which takes the field name and the amount of which we want to implement.

db.a.update({_id:1, {$inc:{X:1}});

Now let’s check the value

db.a.find() 



In the second scenario one client wants to add the field to the document while another client is trying to implement. So we already have our self a record in there with only one field x=10. 

// db.a.save({_id:1, x:10});

Now comes along the client that wants to add the field…we can issue a update command...and again the same client want to increment the value of x we can specify only the x to be incremented as shown below

db.a.update({_id:1},{$set:{y:3}})

db.a.save({_id:1, {$inc:{x:1}})


ok right now we have the fields x and y now if we wanna remove the field y we can issue the unset command the unset command takes the field name and a arbitary value.

db.a.update({_id:1},{$unset:{y:''}})

 or

db.a.update({_id:1},{$unset:{y: 0}})



Conclusion


In this article we have learn about updating documents in mongo database.

Reference

http://www.mongodb.org/ 

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
ramasagar
http://www.ramasagar.com
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)