Database Reference
In-Depth Information
> db.media.save( { "Title" : "Matrix, The"}, {"Type" : "DVD", "Title" :
"Matrix, The", "Released" : "1999", "Genre" : "Action"})
Obviously, this example assumes that the Title value acts as the id field.
Updating Information Automatically
You can use the modifier operations to update information quickly and simply in your
documents, but without needing to type everything in manually. For example, you might
use these operations to increase a number or to remove an element from an array.
We'll be exploring these operators next, providing practical examples that show you
how to use them.
Incrementing a Value with $inc
The $inc operator enables you to perform an (atomic) update on a key to increase the
value by the given increment, assuming that the field exists. If the field doesn't exist, it will
be created. To see this in action, begin by adding another document to the collection:
> manga = ( { "Type" : "Manga", "Title" : "One Piece", "Volumes" : 612,
"Read" : 520 } )
{
"Type" : "Manga",
"Title" : "One Piece",
"Volumes" : "612",
"Read" : "520"
}
> db.media.insert(manga)
Now you're ready to update the document. For example, assume you've read another
four volumes of the One Piece manga, and you want to increment the number of Read
volumes in the document. The following example shows you how to do this:
> db.media.update ( { "Title" : "One Piece"}, {$inc: {"Read" : 4} } )
> db.media.find ( { "Title" : "One Piece" } )
{
"Type" : "Manga",
"Title" : "One Piece ",
"Volumes" : "612",
"Read" : "524"
}
 
Search WWH ::




Custom Search