Introduction to Mongo DB updating Document using findandmodify

Rama Sagar
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 7236 red flag

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 updating Document Part2 before this article.

Introduction

In this article we will learn how to update document in mongo dB using find and modify 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:Introduction to Mongo Database

·         Introduction to Mongo Database

·         Introduction to Mongo DB setting up the server

·         Introduction to Mongo DB Command Line options

·         Introduction to Mongo DB install as a service

·         Introduction to Mongo DB Mongo Shell

·         Introduction to Mongo DB Replica set

·         Introduction to Mongo DB Storing Data 

·         Introduction to Mongo DB Storing Data Part 2

·         Introduction to Mongo DB Updating document

·         Introduction to Mongo DB Updating document Part2             

Objective

The objective of the article is to learn about updating document using find and modify

Find and Modify

In previous article we have seen how to update a document using update command with few options.

If we want to effect exactly one record there is more concise command called find and modify.

Here is the signature of find and modify command


Collection name: - we need to know which collection we are looking into

Document:-we need to look which was the exact document we want to modify

Query order:-sort order of the document if we want to specify multiple documents

What change:-we need to specify what the change we want to make to the document we may want to upsert (set the upsert true) meaning create a new record if one doesn’t exist

Delete it:-we can remove the document which deleted the record.

Return new:-Find and modify also returns the record that we are going to update from the database. By default find and modify returns the version of the before the change is made to it...if we set new to true it will return the document after the change was made to it.

 lets create a query in object mod 

var mod={

   "query" :{

            "things" :1

},

"update":{

"$set" :{

"touched":true

}

},

"sort":{

"_id" : -1

}

} 


The object mod has query finding something that has  things with element 1.it has an update it wanna set the field touched to the value true...and it wanna sort by id in descending order as shown below..


Now if we issue the command 

db.a.findAndModify(mod)

we get the document before it is modified as shown below and the current state is it has the touched field which we have just appended to it.



Now lets set the touched field to false but this time lets return the record after it was modified

mod.new=true

update touched field to be false this time 

mod.update.$set.touched=false



 Now lets find the document

db.a.findAndModify(mod)

It returns the document after the modification as shown below



Now lets sort to find the first record for that we can issue the command

mod.sort._id=1



Now we can see that the document with id 1 has been modified.

Conclusion

In this article we have seen how to use findAndModify in Mongodatabase...we can find lot of information in mongo documents through www.mongodb.org.

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)