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 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); } /** * 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 ($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)* 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();
}
/**
* {@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);
}
}
}