Database Reference
In-Depth Information
"Tracklist" : [
{
"Track" : "1",
"Title" : "You Know You're Right",
"Length" : "3:38"
},
{
"Track" : "5",
"Title" : "Smells Like Teen Spirit",
"Length" : "5:02"
},
{
"Track" : 2,
"Title" : "Been a Son",
"Length" : "2:23"
}
],
"Type" : "CD",
"_id" : ObjectId("4c443ad6c603000000007ed5")
}
It so happens you know that the track number of the most recent item should be 3 rather than 2 . You can use the
$inc method in conjunction with the $ operator to increase the value from 2 to 3 , as in this example:
> db.media.update( { "Tracklist.Title" : "Been a son"},
{$inc:{"Tracklist.$.Track" : 1} } )
Note that only the first item it matches will be updated. Thus, if there are two identical elements in the comments
array, only the first element will be increased.
Atomic Operations
MongoDB supports atomic operations executed against single documents. An atomic operation is a set of operations
that can be combined in such a way that the set of operations appears to be merely one single operation to the rest of
the system. This set of operations will have either a positive or a negative outcome as the final result.
You can call a set of operations an atomic operation if it meets the following pair of conditions:
1.
No other process knows about the changes being made until the entire set of operations
has completed.
2.
If one of the operations fails, the entire set of operations (the entire atomic operation) will
fail, resulting in a full rollback, where the data is restored to its state prior to running the
atomic operation.
 
Search WWH ::




Custom Search