MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
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
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
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
"_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
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
update touched field to be false this time
Now lets find the document
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
Now we can see that the document with id 1 has been modified.
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.