Example #1
0
 /**
  * Execute the mapReduce command.
  *
  * @see Collection::mapReduce()
  * @param string|\MongoCode $map
  * @param string|\MongoCode $reduce
  * @param array|string      $out
  * @param array             $query
  * @param array             $options
  * @return ArrayIterator
  * @throws ResultException if the command fails
  */
 protected function doMapReduce($map, $reduce, $out, array $query, array $options)
 {
     $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options;
     $command = array();
     $command['mapreduce'] = $this->mongoCollection->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = (object) $query;
     $command['out'] = $out;
     $command = array_merge($command, $options);
     foreach (array('map', 'reduce', 'finalize') as $key) {
         if (isset($command[$key]) && is_string($command[$key])) {
             $command[$key] = new \MongoCode($command[$key]);
         }
     }
     $result = $this->database->command($command);
     if (empty($result['ok'])) {
         throw new ResultException($result);
     }
     if (isset($result['result']) && is_string($result['result'])) {
         return $this->database->selectCollection($result['result'])->find();
     }
     if (isset($result['result']) && is_array($result['result']) && isset($result['result']['db'], $result['result']['collection'])) {
         return $this->database->getConnection()->selectCollection($result['result']['db'], $result['result']['collection'])->find();
     }
     $arrayIterator = new ArrayIterator(isset($result['results']) ? $result['results'] : array());
     $arrayIterator->setCommandResult($result);
     return $arrayIterator;
 }
Example #2
0
 public function testSocketTimeoutOptionIsConverted()
 {
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->any())->method('command')->with(['count' => 'foo'], ['socketTimeoutMS' => 1000]);
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $database->command(['count' => 'foo'], ['timeout' => 1000]);
 }
Example #3
0
 /**
  * @see Database::command()
  */
 public function command(array $data, array $options = [], &$hash = null)
 {
     $this->log(['command' => true, 'data' => $data, 'options' => $options]);
     if (func_num_args() > 2) {
         return parent::command($data, $options, $hash);
     }
     return parent::command($data, $options);
 }
 /**
  * @param string   $key
  * @param string   $value
  * @param string   $collectionName
  * @param Database $dataBase
  *
  * @return array
  */
 protected function generateCriteria($key, $value, $collectionName, $dataBase)
 {
     $map = new \MongoCode("function() {\n                    for (var key in eval('this.' + preColumn)) {\n                        emit(preColumn + '.' + key, null);\n                    }\n            }");
     $reduce = new \MongoCode("function(k, vals) {  return null; }");
     preg_match('/(.*)\\.\\*/', $key, $column);
     $preColumnn = $column[1];
     $commandResult = $dataBase->command(array("mapreduce" => $collectionName, "map" => $map, "reduce" => $reduce, "out" => array("inline" => 1), "scope" => array("preColumn" => "{$preColumnn}")));
     $criteria = array();
     if (is_array($commandResult) && array_key_exists('ok', $commandResult) && $commandResult['ok'] == 1) {
         foreach ($commandResult['results'] as $filter) {
             $criteria[] = array($filter['_id'] => $value);
         }
     }
     return $criteria;
 }
Example #5
0
 protected function doMapReduce($map, $reduce, array $query, array $options)
 {
     if (is_string($map)) {
         $map = new \MongoCode($map);
     }
     if (is_string($reduce)) {
         $reduce = new \MongoCode($reduce);
     }
     $command = array();
     $command['mapreduce'] = $this->mongoCollection->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = $query;
     $command = array_merge($command, $options);
     $result = $this->database->command($command);
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     return $this->database->selectCollection($result['result'])->find();
 }
Example #6
0
 protected function doMapReduce($map, $reduce, array $out, array $query, array $options)
 {
     if (is_string($map)) {
         $map = new \MongoCode($map);
     }
     if (is_string($reduce)) {
         $reduce = new \MongoCode($reduce);
     }
     $command = array();
     $command['mapreduce'] = $this->getMongoCollection()->getName();
     $command['map'] = $map;
     $command['reduce'] = $reduce;
     $command['query'] = (object) $query;
     $command['out'] = $out;
     $command = array_merge($command, $options);
     $result = $this->database->command($command);
     if (!$result['ok']) {
         throw new \RuntimeException($result['errmsg']);
     }
     if (isset($out['inline']) && $out['inline'] === true) {
         return new ArrayIterator($result['results']);
     }
     return $this->database->selectCollection($result['result'])->find();
 }
Example #7
0
 public function testSocketTimeoutOptionIsNotConvertedForOlderDrivers()
 {
     if (version_compare(phpversion('mongo'), '1.5.0', '>=')) {
         $this->markTestSkipped('This test is not applicable to driver versions >= 1.5.0');
     }
     $mongoDB = $this->getMockMongoDB();
     $mongoDB->expects($this->any())->method('command')->with(array('count' => 'foo'), array('timeout' => 1000));
     $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager());
     $database->command(array('count' => 'foo'), array('timeout' => 1000));
 }
Example #8
0
 /** @proxy */
 public function command(array $data)
 {
     $this->log(array('command' => true, 'data' => $data));
     return parent::command($data);
 }
Example #9
0
 public function command(array $data, array $options = array())
 {
     $this->log(array('command' => true, 'data' => $data, 'options' => $options));
     return parent::command($data, $options);
 }
 /**
  * @see EntityStore::setupStore
  */
 public function setupStore()
 {
     foreach (MongoDBDocumentBuilder::$SUPPORTED_ENTITY_TYPES as $type) {
         $this->database->command(['collMod' => $type, 'usePowerOf2Sizes' => true]);
     }
 }
Example #11
0
 /**
  * @param Doctrine\MongoDB\Database
  * @param string $file
  *
  * @return array
  *
  * @throws RuntimeException
  * @throws InvalidArgumentException
  * @throws Exception
  */
 public function executeScript(Database $db, $file)
 {
     $scripts = $this->configuration->getMigrationsScriptDirectory();
     if (null === $scripts) {
         throw new \RuntimeException('Missing Configuration for migrations script directory');
     }
     $path = realpath($scripts . '/' . $file);
     if (!file_exists($path)) {
         throw new \InvalidArgumentException(sprintf('Could not execute %s. File does not exist.', $path));
     }
     try {
         $js = file_get_contents($path);
         if (false === $js) {
             throw new \Exception('file_get_contents returned false');
         }
     } catch (\Exception $e) {
         throw $e;
     }
     $result = $db->command(array('$eval' => $js, 'nolock' => true));
     if (isset($result['errmsg'])) {
         throw new \Exception($result['errmsg'], isset($result['errno']) ? $result['errno'] : null);
     }
     return $result;
 }