/** * Execute a database command * * @link http://www.php.net/manual/en/mongodb.command.php * @param array $data The query to send. * @param array $options * @return array Returns database response. */ public function command(array $data, $options = [], &$hash = null) { try { $cursor = new \MongoCommandCursor($this->connection, $this->name, $data); $cursor->setReadPreference($this->getReadPreference()); return iterator_to_array($cursor)[0]; } catch (\MongoDB\Driver\Exception\Exception $e) { return ExceptionConverter::toResultArray($e); } }
/** * Execute an aggregation pipeline command and retrieve results through a cursor * * @link http://php.net/manual/en/mongocollection.aggregatecursor.php * @param array $pipeline * @param array $options * @return MongoCommandCursor */ public function aggregateCursor(array $pipeline, array $options = []) { // Build command manually, can't use mongo-php-library here $command = ['aggregate' => $this->name, 'pipeline' => $pipeline]; // Convert cursor option if (!isset($options['cursor'])) { $options['cursor'] = true; } $command += $options; $cursor = new MongoCommandCursor($this->db->getConnection(), (string) $this, $command); $cursor->setReadPreference($this->getReadPreference()); return $cursor; }
/** * Execute a database command * * @link http://www.php.net/manual/en/mongodb.command.php * @param array $data The query to send. * @param array $options * @return array Returns database response. */ public function command(array $data, $options = [], &$hash = null) { try { $cursor = new \MongoCommandCursor($this->connection, $this->name, $data); $cursor->setReadPreference($this->getReadPreference()); return iterator_to_array($cursor)[0]; } catch (\MongoDB\Driver\Exception\ExecutionTimeoutException $e) { throw new MongoCursorTimeoutException($e->getMessage(), $e->getCode(), $e); } catch (\MongoDB\Driver\Exception\RuntimeException $e) { return ['ok' => 0, 'errmsg' => $e->getMessage(), 'code' => $e->getCode()]; } catch (\MongoDB\Driver\Exception\Excepiton $e) { ExceptionConverter::toLegacy($e); } }