forked from canni/YiiMongoDbSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'devel' into release/1.3.6
- Loading branch information
Showing
3 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
Title: Massive Partial Updates | ||
ShortTitle: Massive Partial updates | ||
Author: Philippe Gaultier <[email protected]> | ||
|
||
--- | ||
|
||
Since the `v1.x.x` You can perform *partial updates* of multiple documents. | ||
|
||
~~~ | ||
[php] | ||
// prepare modifiers | ||
$modifier = new EMongoModifier(); | ||
// replace field1 value with 'new value' | ||
$modifier->addModifier('field1', 'set', 'new value'); | ||
// increment field2 value by 1 | ||
$modifier->addModifier('field2', 'inc', 1); | ||
|
||
// prepare search to find documents | ||
$criteria = new EMongoCriteria(); | ||
$criteria->addCond('field3','==', 'filtered value'); | ||
|
||
// update all matched documents using the modifiers | ||
$status = ModelClass::model()->updateAll($modifier, $criteria); | ||
|
||
~~~ | ||
|
||
And thats it, this will only update those 2 fields (force value of `field1` and increment value of `field2`) | ||
for all the documents having `field3 == 'filtered value'`, everything else in the db will remain untouched. | ||
|
||
--- | ||
|
||
Available modifiers are : | ||
|
||
* inc | ||
* set | ||
* unset | ||
* push | ||
* pushAll | ||
* addToSet | ||
* pop | ||
* pull | ||
* pullAll | ||
* rename | ||
|
||
You can find detailed explanation about usage of those modifiers | ||
on the original [MongoDb documentation](http://www.mongodb.org/display/DOCS/Updating "MongoDB.org"). | ||
|
||
--- | ||
|
||
EMongoModifier can be defined during creation : | ||
|
||
~~~ | ||
[php] | ||
// prepare modifiers | ||
$modifier = new EMongoModifier( | ||
array( | ||
'fieldName1'=>array('inc' => $incValue), | ||
'fieldName2'=>array('set' => $targetValue), | ||
'fieldName3'=>array('unset' => 1), | ||
'fieldName4'=>array('push' => $pushedValue), | ||
'fieldName5'=>array('pushAll' => array($pushedValue1, $pushedValue2)), | ||
'fieldName6'=>array('addToSet' => $addedValue), | ||
'fieldName7'=>array('pop' => 1), | ||
'fieldName8'=>array('pop' => -1), | ||
'fieldName9'=>array('pull' => $removedValue), | ||
'fieldName10'=>array('pullAll' => array($removedValue1, $removedValue2)), | ||
'fieldName11'=>array('rename' => $newFieldName), | ||
) | ||
); | ||
|
||
~~~ | ||
|
||
, during execution | ||
|
||
~~~ | ||
[php] | ||
$modifier = new EMongoModifier(); | ||
$modifier->addCond($fieldName1, 'inc', $incValue), | ||
$modifier->addCond($fieldName2, 'set', $targetValue), | ||
$modifier->addCond($fieldName3, 'unset', 1), | ||
$modifier->addCond($fieldName4, 'push', $pushedValue), | ||
$modifier->addCond($fieldName5, 'pushAll', array($pushedValue1, $pushedValue2)), | ||
$modifier->addCond($fieldName6, 'addToSet', $addedValue), | ||
$modifier->addCond($fieldName7, 'pop', 1), | ||
$modifier->addCond($fieldName8, 'pop', -1), | ||
$modifier->addCond($fieldName9, 'pull', $removedValue), | ||
$modifier->addCond($fieldName10, 'pullAll', array($removedValue1, $removedValue2)), | ||
$modifier->addCond($fieldName11, 'rename', $newFieldName), | ||
|
||
~~~ | ||
|
||
or using the two methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters