connection = $conn;
$this->name = $name;
$this->db = $this->connection->getClient()->selectDatabase($name);
}
/**
* @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;
}
/**
* (PECL mongo >= 1.0.2)
* 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)
{
return $this->selectCollection($name);
}
/**
* (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 = [])
{
if (is_bool($options)) {
$options = ['includeSystemCollections' => $options];
}
$collections = $this->db->listCollections($options);
$getCollectionName = function (CollectionInfo $collectionInfo) {
return $collectionInfo->getName();
};
return array_map($getCollectionName, (array) $collections);
}
/**
* (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()
{
return static::PROFILING_OFF;
}
/**
* (PECL mongo >= 1.1.0)
* Get slaveOkay setting for this database
* @link http://www.php.net/manual/en/mongodb.getslaveokay.php
* @return bool Returns the value of slaveOkay for this instance.
*/
public function getSlaveOkay()
{
return false;
}
/**
* (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)
{
return static::PROFILING_OFF;
}
/**
* (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 arrayReturns db response.
*/ public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE) { return []; } /** * (PECL mongo >= 0.9.0)* Returns a new collection object. *
*/ public function selectCollection($name) { return new MongoCollection($this, $name); } /** * (PECL mongo >= 1.1.0)* If reads should be sent to secondary members of a replica set for all * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance. *
* @return bool Returns the former value of slaveOkay for this instance. */ public function setSlaveOkay ($ok = true) { return false; } /** * 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 MongoCollectionReturns 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)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)* 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 arrayReturns 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 (is_object($document_or_id)) { $id = isset($document_or_id->_id) ? $document_or_id->_id : null; // $id = $document_or_id->_id ?? null; } 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 [ '$ref' => $collection, '$id' => $id, '$db' => $this->name, ]; } /** * (PECL mongo >= 0.9.0)This function returns an array describing the write concern. * The array contains the values w for an integer acknowledgement level or string mode, * and wtimeout denoting the maximum number of milliseconds to wait for the server to satisfy the write concern.
*/ public function getWriteConcern() { $this->notImplemented(); } /** * (PECL mongo >= 0.9.3)* This parameter is an associative array of the form * array("optionname" => <boolean>, ...). Currently * supported options are: *
"timeout"
Deprecated alias for "socketTimeoutMS".
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();
}
/**
* (PECL mongo >= 1.3.0)
* Get the read preference for this database
* @link http://www.php.net/manual/en/mongodb.getreadpreference.php
* @return array This function returns an array describing the read preference. The array contains the values type for the string read preference mode (corresponding to the MongoClient constants), and tagsets containing a list of all tag set criteria. If no tag sets were specified, tagsets will not be present in the array.
*/
public function getReadPreference()
{
$this->notImplemented();
}
/**
* (PECL mongo >= 1.3.0)
* Set the read preference for this database
* @link http://www.php.net/manual/en/mongodb.setreadpreference.php
* @param string $read_preference The read preference mode: MongoClient::RP_PRIMARY, MongoClient::RP_PRIMARY_PREFERRED, MongoClient::RP_SECONDARY, MongoClient::RP_SECONDARY_PREFERRED, or MongoClient::RP_NEAREST.
* @param array $tags [optional] An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members.
* @return boolean Returns TRUE on success, or FALSE otherwise.
*/
public function setReadPreference($read_preference, array $tags)
{
$this->notImplemented();
}
/**
* (PECL mongo >= 1.5.0)
* @link http://php.net/manual/en/mongodb.setwriteconcern.php
* Set the write concern for this database
* @param mixed $w The write concern. This may be an integer denoting the number of servers required to acknowledge the write, or a string mode (e.g. "majority").
* @param int $wtimeout[optional] The maximum number of milliseconds to wait for the server to satisfy the write concern.
* @return boolean Returns TRUE on success, or FALSE otherwise.
*/
public function setWriteConcern($w, $wtimeout)
{
$this->notImplemented();
}
protected function notImplemented()
{
throw new \Exception('Not implemented');
}
}