connection = $conn; $this->name = $name; $this->setReadPreferenceFromArray($conn->getReadPreference()); $this->setWriteConcernFromArray($conn->getWriteConcern()); $this->createDatabaseObject(); } /** * @return \MongoDB\Database * @internal */ public function getDb() { return $this->db; } /** * The name of this database * @link http://www.php.net/manual/en/mongodb.--tostring.php * @return string Returns this database's name. */ public function __toString() { return $this->name; } /** * Gets a collection * * @link http://www.php.net/manual/en/mongodb.get.php * @param string $name The name of the collection. * @return MongoCollection */ public function __get($name) { // Handle w and wtimeout properties that replicate data stored in $readPreference if ($name === 'w' || $name === 'wtimeout') { return $this->getWriteConcern()[$name]; } return $this->selectCollection($name); } /** * @param string $name * @param mixed $value */ public function __set($name, $value) { if ($name === 'w' || $name === 'wtimeout') { $this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern()); $this->createDatabaseObject(); } } /** * (PECL mongo >= 1.3.0)
* @link http://www.php.net/manual/en/mongodb.getcollectionnames.php * Get all collections from this database * @return array Returns the names of the all the collections in the database as an * {@link http://www.php.net/manual/en/language.types.array.php array}. */ public function getCollectionNames(array $options = []) { // The includeSystemCollections option is no longer supported if (isset($options['includeSystemCollections'])) { unset($options['includeSystemCollections']); } $collections = $this->db->listCollections($options); $getCollectionName = function (CollectionInfo $collectionInfo) { return $collectionInfo->getName(); }; return array_map($getCollectionName, (array)$collections); } /** * @return MongoClient * @internal This method is not part of the ext-mongo API */ public function getConnection() { return $this->connection; } /** * (PECL mongo >= 0.9.0)
* Fetches toolkit for dealing with files stored in this database * @link http://www.php.net/manual/en/mongodb.getgridfs.php * @param string $prefix [optional] The prefix for the files and chunks collections. * @return MongoGridFS Returns a new gridfs object for this database. */ public function getGridFS($prefix = "fs") { return new \MongoGridFS($this, $prefix, $prefix); } /** * (PECL mongo >= 0.9.0)
* Gets this database's profiling level * @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php * @return int Returns the profiling level. */ public function getProfilingLevel() { $result = $this->command(['profile' => -1]); return ($result['ok'] && isset($result['was'])) ? $result['was'] : 0; } /** * (PECL mongo >= 0.9.0)
* Sets this database's profiling level * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php * @param int $level Profiling level. * @return int Returns the previous profiling level. */ public function setProfilingLevel($level) { $result = $this->command(['profile' => $level]); return ($result['ok'] && isset($result['was'])) ? $result['was'] : 0; } /** * (PECL mongo >= 0.9.0)
* Drops this database * @link http://www.php.net/manual/en/mongodb.drop.php * @return array Returns the database response. */ public function drop() { return $this->db->drop(); } /** * Repairs and compacts this database * @link http://www.php.net/manual/en/mongodb.repair.php * @param bool $preserve_cloned_files [optional]

If cloned files should be kept if the repair fails.

* @param bool $backup_original_files [optional]

If original files should be backed up.

* @return array

Returns db response.

*/ public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE) { return []; } /** * (PECL mongo >= 0.9.0)
* Gets a collection * @link http://www.php.net/manual/en/mongodb.selectcollection.php * @param string $name The collection name. * @throws Exception if the collection name is invalid. * @return MongoCollection

* Returns a new collection object. *

*/ public function selectCollection($name) { return new MongoCollection($this, $name); } /** * Creates a collection * @link http://www.php.net/manual/en/mongodb.createcollection.php * @param string $name The name of the collection. * @param array $options [optional]

*

* An array containing options for the collections. Each option is its own * element in the options array, with the option name listed below being * the key of the element. The supported options depend on the MongoDB * server version. At the moment, the following options are supported: *

*

* capped *

* If the collection should be a fixed size. *

*

*

* size *

* If the collection is fixed size, its size in bytes.

*

max *

If the collection is fixed size, the maximum number of elements to store in the collection.

* autoIndexId * *

* If capped is TRUE you can specify FALSE to disable the * automatic index created on the _id field. * Before MongoDB 2.2, the default value for * autoIndexId was FALSE. *

*

* @return MongoCollection

Returns a collection object representing the new collection.

*/ public function createCollection($name, $options) { $this->db->createCollection($name, $options); return $this->selectCollection($name); } /** * (PECL mongo >= 0.9.0)
* @deprecated Use MongoCollection::drop() instead. * Drops a collection * @link http://www.php.net/manual/en/mongodb.dropcollection.php * @param MongoCollection|string $coll MongoCollection or name of collection to drop. * @return array Returns the database response. */ public function dropCollection($coll) { return $this->db->dropCollection((string) $coll); } /** * (PECL mongo >= 0.9.0)
* Get a list of collections in this database * @link http://www.php.net/manual/en/mongodb.listcollections.php * @param bool $includeSystemCollections [optional]

Include system collections.

* @return array Returns a list of MongoCollections. */ public function listCollections(array $options = []) { return array_map([$this, 'selectCollection'], $this->getCollectionNames($options)); } /** * (PECL mongo >= 0.9.0)
* Creates a database reference * @link http://www.php.net/manual/en/mongodb.createdbref.php * @param string $collection The collection to which the database reference will point. * @param mixed $document_or_id

* If an array or object is given, its _id field will be * used as the reference ID. If a {@see MongoId} or scalar * is given, it will be used as the reference ID. *

* @return array

Returns a database reference array.

*

* If an array without an _id field was provided as the * document_or_id parameter, NULL will be returned. *

*/ public function createDBRef($collection, $document_or_id) { if ($document_or_id instanceof \MongoId) { $id = $document_or_id; } elseif (is_object($document_or_id)) { if (! isset($document_or_id->_id)) { return null; } $id = $document_or_id->_id; } elseif (is_array($document_or_id)) { if (! isset($document_or_id['_id'])) { return null; } $id = $document_or_id['_id']; } else { $id = $document_or_id; } return MongoDBRef::create($collection, $id, $this->name); } /** * (PECL mongo >= 0.9.0)
* Fetches the document pointed to by a database reference * @link http://www.php.net/manual/en/mongodb.getdbref.php * @param array $ref A database reference. * @return array Returns the document pointed to by the reference. */ public function getDBRef(array $ref) { return MongoDBRef::get($this, $ref); } /** * (PECL mongo >= 0.9.3)
* Runs JavaScript code on the database server. * @link http://www.php.net/manual/en/mongodb.execute.php * @param MongoCode|string $code Code to execute. * @param array $args [optional] Arguments to be passed to code. * @return array Returns the result of the evaluation. */ public function execute($code, array $args = array()) { $this->notImplemented(); } /** * Execute a database command * @link http://www.php.net/manual/en/mongodb.command.php * @param array $data The query to send. * @param array() $options [optional]

* This parameter is an associative array of the form * array("optionname" => <boolean>, ...). Currently * supported options are: *

* @return array Returns database response. * Every database response is always maximum one document, * which means that the result of a database command can never exceed 16MB. * The resulting document's structure depends on the command, * but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents. */ 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\RuntimeException $e) { return [ 'ok' => 0, 'errmsg' => $e->getMessage(), 'code' => $e->getCode(), ]; } } /** * (PECL mongo >= 0.9.5)
* Check if there was an error on the most recent db operation performed * @link http://www.php.net/manual/en/mongodb.lasterror.php * @return array Returns the error, if there was one. */ public function lastError() { $this->notImplemented(); } /** * (PECL mongo >= 0.9.5)
* Checks for the last error thrown during a database operation * @link http://www.php.net/manual/en/mongodb.preverror.php * @return array Returns the error and the number of operations ago it occurred. */ public function prevError() { $this->notImplemented(); } /** * (PECL mongo >= 0.9.5)
* Clears any flagged errors on the database * @link http://www.php.net/manual/en/mongodb.reseterror.php * @return array Returns the database response. */ public function resetError() { $this->notImplemented(); } /** * (PECL mongo >= 0.9.5)
* Creates a database error * @link http://www.php.net/manual/en/mongodb.forceerror.php * @return boolean Returns the database response. */ public function forceError() { $this->notImplemented(); } /** * (PECL mongo >= 1.0.1)
* Log in to this database * @link http://www.php.net/manual/en/mongodb.authenticate.php * @param string $username The username. * @param string $password The password (in plaintext). * @return array

Returns database response. If the login was successful, it will return 1.

*

* <?php
array("ok" => 1);
?> * * * *

*

If something went wrong, it will return

*

*

*
* <?php
array("ok" => 0"errmsg" => "auth fails");
?>

*

("auth fails" could be another message, depending on database version and * what went wrong)

*/ public function authenticate($username, $password) { $this->notImplemented(); } /** * {@inheritdoc} */ public function setReadPreference($readPreference, $tags = null) { $result = $this->setReadPreferenceFromParameters($readPreference, $tags); $this->createDatabaseObject(); return $result; } /** * {@inheritdoc} */ public function setWriteConcern($wstring, $wtimeout = 0) { $result = $this->setWriteConcernFromParameters($wstring, $wtimeout); $this->createDatabaseObject(); return $result; } protected function notImplemented() { throw new \Exception('Not implemented'); } /** * @return \MongoDB\Database */ private function createDatabaseObject() { $options = [ 'readPreference' => $this->readPreference, 'writeConcern' => $this->writeConcern, ]; if ($this->db === null) { $this->db = $this->connection->getClient()->selectDatabase($this->name, $options); } else { $this->db = $this->db->withOptions($options); } } }