/** * @expectedException \Sokil\Mongo\Exception * @expectedExceptionMessage Error setting write concern */ public function testSetWriteConcern_Error() { $mongoDatabaseMock = $this->getMock('\\MongoDB', array('setWriteConcern'), array($this->database->getClient()->getMongoClient(), 'test')); $mongoDatabaseMock->expects($this->once())->method('setWriteConcern')->will($this->returnValue(false)); $database = new Database($this->database->getClient(), $mongoDatabaseMock); $database->setWriteConcern(1); }
/** * Aggregate using pipeline * * @param callable|array|\Sokil\Mongo\Pipeline $pipeline list of pipeline stages * @link http://docs.mongodb.org/manual/reference/operator/aggregation/ * @return array result of aggregation * @throws \Sokil\Mongo\Exception */ public function aggregate($pipeline) { // configure through callable if (is_callable($pipeline)) { $pipelineConfiguratorCallable = $pipeline; $pipeline = $this->createAggregator(); call_user_func($pipelineConfiguratorCallable, $pipeline); } // get aggregation array if ($pipeline instanceof Pipeline) { $pipeline = $pipeline->toArray(); } elseif (!is_array($pipeline)) { throw new Exception('Wrong pipeline specified'); } // log $client = $this->_database->getClient(); if ($client->hasLogger()) { $client->getLogger()->debug(get_called_class() . ':<br><b>Pipeline</b>:<br>' . json_encode($pipeline)); } // aggregate $status = $this->_database->executeCommand(array('aggregate' => $this->getName(), 'pipeline' => $pipeline)); if ($status['ok'] != 1) { throw new Exception('Aggregate error: ' . $status['errmsg']); } return $status['result']; }
/** * Aggregate using pipeline * @link http://docs.mongodb.org/manual/reference/operator/aggregation/ * * @param callable|array|Pipeline $pipeline list of pipeline stages * @param array aggregate options * @param bool $asCursor return result as cursor * * @throws \Sokil\Mongo\Exception * @return array result of aggregation */ public function aggregate($pipeline, array $options = array(), $asCursor = false) { // configure through callable if (is_callable($pipeline)) { $pipelineConfiguratorCallable = $pipeline; $pipeline = $this->createAggregator(); call_user_func($pipelineConfiguratorCallable, $pipeline); } // get aggregation array if ($pipeline instanceof Pipeline) { if ($options && is_array($options)) { $options = array_merge($pipeline->getOptions(), $options); } else { $options = $pipeline->getOptions(); } $pipeline = $pipeline->toArray(); } else { if (!is_array($pipeline)) { throw new Exception('Wrong pipeline specified'); } } // log $client = $this->database->getClient(); if ($client->isDebugEnabled()) { // record pipeline if ($client->hasLogger()) { $client->getLogger()->debug(get_called_class() . ':<br><b>Pipeline</b>:<br>' . json_encode($pipeline)); } // Check options only in debug mode. In production common exception will raised if ($options) { // get db version $dbVersion = $client->getDbVersion(); // check options for db < 2.6 if (version_compare($dbVersion, '2.6.0', '<')) { if (!empty($options['explain'])) { throw new FeatureNotSupportedException('Explain of aggregation implemented only from 2.6.0'); } if (!empty($options['allowDiskUse'])) { throw new FeatureNotSupportedException('Option allowDiskUse of aggregation implemented only from 2.6.0'); } if (!empty($options['cursor'])) { throw new FeatureNotSupportedException('Option cursor of aggregation implemented only from 2.6.0'); } } // check options for db < 3.2 if (version_compare($dbVersion, '3.2.0', '<')) { if (!empty($options['bypassDocumentValidation'])) { throw new FeatureNotSupportedException('Option bypassDocumentValidation of aggregation implemented only from 3.2.0'); } if (!empty($options['readConcern'])) { throw new FeatureNotSupportedException('Option readConcern of aggregation implemented only from 3.2.0'); } } } } // return result as cursor if ($asCursor) { if (version_compare(\MongoClient::VERSION, '1.5.0', '<')) { throw new FeatureNotSupportedException('Aggregate cursor supported from driver version 1.5'); } $cursor = $this->getMongoCollection()->aggregateCursor($pipeline, $options); return $cursor; } // prepare command $command = array('aggregate' => $this->getName(), 'pipeline' => $pipeline); // add options if ($options) { $command += $options; } // aggregate $status = $this->database->executeCommand($command); if ($status['ok'] != 1) { throw new Exception('Aggregate error: ' . $status['errmsg']); } // explain response if (!empty($command['explain'])) { return $status['stages']; } // result response return $status['result']; }