Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <pre><?php
- $_start = microtime(1);
- $mongo = new MongoPlus;
- #var_dump($mongo);
- $db = $mongo->testdb;
- #var_dump($db);
- $collection = $db->testtable;
- #var_dump($collection);
- // start over
- // drop table
- $collection->drop();
- // insert 5 random records
- repeat(5, function() use ($collection) {
- $collection->insert(array(
- 'name' => array(
- 'first' => 'Voor' . rand(0, 999) . 'naamz',
- 'last' => 'Lastest ' . rand(0, 999),
- ),
- 'email' => 'user-' . rand(0, 999) . '@onderstebuiten.nl',
- 'some_number' => rand(0, 999),
- ));
- });
- // query, alter & query new data
- // show pre
- $all = $collection->find()->limit(2);
- #var_dump($all->count());
- var_dump($all);
- print_r($pre = iterator_to_array($all));
- // do manual 'correct' update
- if ( 0 ) {
- $id = key($pre);
- var_dump($id);
- var_dump($collection->update(array('_id' => new MongoId($id)), array('a' => 'b'), array('multiple' => false)));
- }
- // do updates
- if ( 0 ) {
- $all = $collection->find()->limit(2);
- var_dump($all->update(function( $doc ) {
- return array(
- '$set' => array(
- 'new_field1' => rand(0, 999),
- 'some_number' => $doc['some_number'] + 3
- ),
- '$addToSet' => array(
- 'new_field2' => rand(0, 999)
- ),
- );
- }));
- }
- // StackOverflow example code
- if ( 1 ) {
- // query
- $docs = $collection->find()->limit(2);
- // fetch
- foreach ( $docs AS $id => $doc ) {
- // update
- $collection->update(array('_id' => $doc['_id']), array(
- '$set' => array(
- 'some_number' => 'x',
- 'new_field' => 'y',
- )
- ), array('multiple' => false));
- }
- }
- // show post
- $all = $collection->find()->limit(2);
- print_r(iterator_to_array($all));
- echo "\n" . number_format(microtime(1) - $_start, 4) . "\n";
- function repeat( $amount, Closure $callback, $arguments = array() ) {
- for ( $i=0; $i<$amount; $i++ ) {
- call_user_func_array($callback, $arguments);
- }
- }
- class MongoPlus extends Mongo {
- public function __get( $name ) {
- return $this->selectDB($name);
- }
- public function selectDB( $name ) {
- return new MongoDBPlus($this, $name);
- }
- }
- class MongoDBPlus extends MongoDB {
- public $_mongo;
- // public $_mongo_id;
- public function __construct( MongoPlus $mongo, $name ) {
- $this->_mongo = $mongo;
- // $this->_mongo_id = md5(microtime(1));
- // $this->ref_mongo($mongo);
- parent::__construct($mongo, $name);
- }
- /* public function ref_mongo( MongoPlus $mongo = null ) {
- static $mongos;
- if ( $mongo ) {
- $mongos[$this->_mongo_id] = $mongo;
- }
- return $mongos[$this->_mongo_id];
- }*/
- public function __get( $name ) {
- return $this->selectCollection($name);
- }
- public function selectCollection( $name ) {
- return new MongoCollectionPlus($this, $name);
- }
- }
- class MongoCollectionPlus extends MongoCollection {
- public function find( $query = array(), $fields = array() ) {
- return new MongoCursorPlus($this->db->_mongo, (string)$this, $query, $fields, $this);
- }
- }
- class MongoCursorPlus extends MongoCursor {
- public $collection;
- public function __construct( $mongo, $ns, $query, $fields, $collection ) {
- $this->_collection = $collection;
- parent::__construct($mongo, $ns, $query, $fields);
- }
- public function update( Closure $callback, $options = array() ) {
- isset($options['multiple']) or $options['multiple'] = false;
- $updated = array();
- foreach ( $this AS $i => $doc ) {
- $update = $callback($doc);
- $id = $doc['_id'];
- // $id = $id->{'$id'}; // invalid -- _id must be typeof MongoId
- // $id = new MongoId($id->{'$id'}); // valid -- but unnecessary, because $doc['_id'] is just that
- $updated[$i] = $this->_collection->update(array('_id' => $id), $update, $options);
- }
- return $updated;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement