advanced.partial-batch-update.txt 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Title: Massive Partial Updates
  2. Author: Philippe Gaultier <pgaultier@gmail.com>
  3. ---
  4. Since the `v1.3.6` You can perform *partial updates* of multiple documents.
  5. ~~~
  6. [php]
  7. // prepare modifiers
  8. $modifier = new EMongoModifier();
  9. // replace field1 value with 'new value'
  10. $modifier->addModifier('field1', 'set', 'new value');
  11. // increment field2 value by 1
  12. $modifier->addModifier('field2', 'inc', 1);
  13. // prepare search to find documents
  14. $criteria = new EMongoCriteria();
  15. $criteria->addCond('field3','==', 'filtered value');
  16. // update all matched documents using the modifiers
  17. $status = ModelClass::model()->updateAll($modifier, $criteria);
  18. ~~~
  19. And thats it, this will only update those 2 fields (force value of `field1` and increment value of `field2`)
  20. for all the documents having `field3 == 'filtered value'`, everything else in the db will remain untouched.
  21. ---
  22. Available modifiers are :
  23. * inc
  24. * set
  25. * unset
  26. * push
  27. * pushAll
  28. * addToSet
  29. * pop
  30. * pull
  31. * pullAll
  32. * rename
  33. You can find detailed explanation about usage of those modifiers
  34. on the original [MongoDb documentation](http://www.mongodb.org/display/DOCS/Updating "MongoDB.org").
  35. ---
  36. EMongoModifier can be defined during creation :
  37. ~~~
  38. [php]
  39. // prepare modifiers
  40. $modifier = new EMongoModifier(
  41. array(
  42. 'fieldName1'=>array('inc' => $incValue),
  43. 'fieldName2'=>array('set' => $targetValue),
  44. 'fieldName3'=>array('unset' => 1),
  45. 'fieldName4'=>array('push' => $pushedValue),
  46. 'fieldName5'=>array('pushAll' => array($pushedValue1, $pushedValue2)),
  47. 'fieldName6'=>array('addToSet' => $addedValue),
  48. 'fieldName7'=>array('pop' => 1),
  49. 'fieldName8'=>array('pop' => -1),
  50. 'fieldName9'=>array('pull' => $removedValue),
  51. 'fieldName10'=>array('pullAll' => array($removedValue1, $removedValue2)),
  52. 'fieldName11'=>array('rename' => $newFieldName),
  53. )
  54. );
  55. ~~~
  56. , during execution
  57. ~~~
  58. [php]
  59. $modifier = new EMongoModifier();
  60. $modifier->addCond($fieldName1, 'inc', $incValue),
  61. $modifier->addCond($fieldName2, 'set', $targetValue),
  62. $modifier->addCond($fieldName3, 'unset', 1),
  63. $modifier->addCond($fieldName4, 'push', $pushedValue),
  64. $modifier->addCond($fieldName5, 'pushAll', array($pushedValue1, $pushedValue2)),
  65. $modifier->addCond($fieldName6, 'addToSet', $addedValue),
  66. $modifier->addCond($fieldName7, 'pop', 1),
  67. $modifier->addCond($fieldName8, 'pop', -1),
  68. $modifier->addCond($fieldName9, 'pull', $removedValue),
  69. $modifier->addCond($fieldName10, 'pullAll', array($removedValue1, $removedValue2)),
  70. $modifier->addCond($fieldName11, 'rename', $newFieldName),
  71. ~~~
  72. or using the two methods