Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <pre><?php
  2.  
  3. $_start = microtime(1);
  4.  
  5. $mongo = new MongoPlus;
  6. #var_dump($mongo);
  7. $db = $mongo->testdb;
  8. #var_dump($db);
  9. $collection = $db->testtable;
  10. #var_dump($collection);
  11.  
  12.  
  13. // start over
  14.  
  15. // drop table
  16. $collection->drop();
  17.  
  18. // insert 5 random records
  19. repeat(5, function() use ($collection) {
  20.     $collection->insert(array(
  21.         'name' => array(
  22.             'first' => 'Voor' . rand(0, 999) . 'naamz',
  23.             'last' => 'Lastest ' . rand(0, 999),
  24.         ),
  25.         'email' => 'user-' . rand(0, 999) . '@onderstebuiten.nl',
  26.         'some_number' => rand(0, 999),
  27.     ));
  28. });
  29.  
  30.  
  31. // query, alter & query new data
  32.  
  33. // show pre
  34. $all = $collection->find()->limit(2);
  35. #var_dump($all->count());
  36. var_dump($all);
  37. print_r($pre = iterator_to_array($all));
  38.  
  39. // do manual 'correct' update
  40. if ( 0 ) {
  41.     $id = key($pre);
  42.     var_dump($id);
  43.     var_dump($collection->update(array('_id' => new MongoId($id)), array('a' => 'b'), array('multiple' => false)));
  44. }
  45.  
  46. // do updates
  47. if ( 0 ) {
  48.     $all = $collection->find()->limit(2);
  49.     var_dump($all->update(function( $doc ) {
  50.         return array(
  51.             '$set' => array(
  52.                 'new_field1' => rand(0, 999),
  53.                 'some_number' => $doc['some_number'] + 3
  54.             ),
  55.             '$addToSet' => array(
  56.                 'new_field2' => rand(0, 999)
  57.             ),
  58.         );
  59.     }));
  60. }
  61.  
  62. // StackOverflow example code
  63. if ( 1 ) {
  64.     // query
  65.     $docs = $collection->find()->limit(2);
  66.     // fetch
  67.     foreach ( $docs AS $id => $doc ) {
  68.         // update
  69.         $collection->update(array('_id' => $doc['_id']), array(
  70.             '$set' => array(
  71.                 'some_number' => 'x',
  72.                 'new_field' => 'y',
  73.             )
  74.         ), array('multiple' => false));
  75.     }
  76. }
  77.  
  78. // show post
  79. $all = $collection->find()->limit(2);
  80. print_r(iterator_to_array($all));
  81.  
  82.  
  83.  
  84. echo "\n" . number_format(microtime(1) - $_start, 4) . "\n";
  85.  
  86.  
  87.  
  88. function repeat( $amount, Closure $callback, $arguments = array() ) {
  89.     for ( $i=0; $i<$amount; $i++ ) {
  90.         call_user_func_array($callback, $arguments);
  91.     }
  92. }
  93.  
  94. class MongoPlus extends Mongo {
  95.     public function __get( $name ) {
  96.         return $this->selectDB($name);
  97.     }
  98.     public function selectDB( $name ) {
  99.         return new MongoDBPlus($this, $name);
  100.     }
  101. }
  102.  
  103. class MongoDBPlus extends MongoDB {
  104.     public $_mongo;
  105. //  public $_mongo_id;
  106.     public function __construct( MongoPlus $mongo, $name ) {
  107.         $this->_mongo = $mongo;
  108. //      $this->_mongo_id = md5(microtime(1));
  109. //      $this->ref_mongo($mongo);
  110.         parent::__construct($mongo, $name);
  111.     }
  112. /*  public function ref_mongo( MongoPlus $mongo = null ) {
  113.         static $mongos;
  114.         if ( $mongo ) {
  115.             $mongos[$this->_mongo_id] = $mongo;
  116.         }
  117.         return $mongos[$this->_mongo_id];
  118.     }*/
  119.     public function __get( $name ) {
  120.         return $this->selectCollection($name);
  121.     }
  122.     public function selectCollection( $name ) {
  123.         return new MongoCollectionPlus($this, $name);
  124.     }
  125. }
  126.  
  127. class MongoCollectionPlus extends MongoCollection {
  128.     public function find( $query = array(), $fields = array() ) {
  129.         return new MongoCursorPlus($this->db->_mongo, (string)$this, $query, $fields, $this);
  130.     }
  131. }
  132.  
  133. class MongoCursorPlus extends MongoCursor {
  134.     public $collection;
  135.     public function __construct( $mongo, $ns, $query, $fields, $collection ) {
  136.         $this->_collection = $collection;
  137.         parent::__construct($mongo, $ns, $query, $fields);
  138.     }
  139.     public function update( Closure $callback, $options = array() ) {
  140.         isset($options['multiple']) or $options['multiple'] = false;
  141.         $updated = array();
  142.         foreach ( $this AS $i => $doc ) {
  143.             $update = $callback($doc);
  144.             $id = $doc['_id'];
  145. //          $id = $id->{'$id'}; // invalid -- _id must be typeof MongoId
  146. //          $id = new MongoId($id->{'$id'}); // valid -- but unnecessary, because $doc['_id'] is just that
  147.             $updated[$i] = $this->_collection->update(array('_id' => $id), $update, $options);
  148.         }
  149.         return $updated;
  150.     }
  151. }
  152.  
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement