Class yii\mongodb\Database

Inheritanceyii\mongodb\Database » yii\base\Object
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/Database.php

Database represents the Mongo database information.

Public Properties

Hide inherited properties

Property Type Description Defined By
$fileCollection yii\mongodb\file\Collection Mongo GridFS collection. yii\mongodb\Database
$mongoDb \MongoDB Mongo database instance. yii\mongodb\Database
$name string Name of this database. yii\mongodb\Database

Public Methods

Hide inherited methods

Method Description Defined By
createCollection() Creates new collection. yii\mongodb\Database
executeCommand() Executes Mongo command. yii\mongodb\Database
getCollection() Returns the Mongo collection with the given name. yii\mongodb\Database
getFileCollection() Returns Mongo GridFS collection with given prefix. yii\mongodb\Database
getName() yii\mongodb\Database

Protected Methods

Hide inherited methods

Method Description Defined By
selectCollection() Selects collection with given name. yii\mongodb\Database
selectFileCollection() Selects GridFS collection with given prefix. yii\mongodb\Database
tryResultError() Checks if command execution result ended with an error. yii\mongodb\Database

Property Details

Hide inherited properties

$fileCollection public property

Mongo GridFS collection. This property is read-only.

$mongoDb public property

Mongo database instance.

public \MongoDB $mongoDb null
$name public property

Name of this database. This property is read-only.

public string $name null

Method Details

Hide inherited methods

createCollection() public method

Creates new collection.

Note: Mongo creates new collections automatically on the first demand, this method makes sense only for the migration script or for the case you need to create collection with the specific options.

public \MongoCollection createCollection ( $name, $options = [] )
$name string

Name of the collection

$options array

Collection options in format: "name" => "value"

return \MongoCollection

New Mongo collection instance.

throws yii\mongodb\Exception

on failure.

                public function createCollection($name, $options = [])
{
    $token = $this->getName() . '.create(' . $name . ', ' . Json::encode($options) . ')';
    Yii::info($token, __METHOD__);
    try {
        Yii::beginProfile($token, __METHOD__);
        $result = $this->mongoDb->createCollection($name, $options);
        Yii::endProfile($token, __METHOD__);
        return $result;
    } catch (\Exception $e) {
        Yii::endProfile($token, __METHOD__);
        throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
    }
}

            
executeCommand() public method

Executes Mongo command.

public array executeCommand ( $command, $options = [] )
$command array

Command specification.

$options array

Options in format: "name" => "value"

return array

Database response.

throws yii\mongodb\Exception

on failure.

                public function executeCommand($command, $options = [])
{
    $token = $this->getName() . '.$cmd(' . Json::encode($command) . ', ' . Json::encode($options) . ')';
    Yii::info($token, __METHOD__);
    try {
        Yii::beginProfile($token, __METHOD__);
        $result = $this->mongoDb->command($command, $options);
        $this->tryResultError($result);
        Yii::endProfile($token, __METHOD__);
        return $result;
    } catch (\Exception $e) {
        Yii::endProfile($token, __METHOD__);
        throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
    }
}

            
getCollection() public method

Returns the Mongo collection with the given name.

public yii\mongodb\Collection getCollection ( $name, $refresh false )
$name string

Collection name

$refresh boolean

Whether to reload the collection instance even if it is found in the cache.

return yii\mongodb\Collection

Mongo collection instance.

                public function getCollection($name, $refresh = false)
{
    if ($refresh || !array_key_exists($name, $this->_collections)) {
        $this->_collections[$name] = $this->selectCollection($name);
    }
    return $this->_collections[$name];
}

            
getFileCollection() public method

Returns Mongo GridFS collection with given prefix.

public yii\mongodb\file\Collection getFileCollection ( $prefix 'fs', $refresh false )
$prefix string

Collection prefix.

$refresh boolean

Whether to reload the collection instance even if it is found in the cache.

return yii\mongodb\file\Collection

Mongo GridFS collection.

                public function getFileCollection($prefix = 'fs', $refresh = false)
{
    if ($refresh || !array_key_exists($prefix, $this->_fileCollections)) {
        $this->_fileCollections[$prefix] = $this->selectFileCollection($prefix);
    }
    return $this->_fileCollections[$prefix];
}

            
getName() public method

public string getName ( )
return string

Name of this database.

                public function getName()
{
    return $this->mongoDb->__toString();
}

            
selectCollection() protected method

Selects collection with given name.

protected yii\mongodb\Collection selectCollection ( $name )
$name string

Collection name.

return yii\mongodb\Collection

Collection instance.

                protected function selectCollection($name)
{
    return Yii::createObject([
        'class' => 'yii\mongodb\Collection',
        'mongoCollection' => $this->mongoDb->selectCollection($name)
    ]);
}

            
selectFileCollection() protected method

Selects GridFS collection with given prefix.

protected yii\mongodb\file\Collection selectFileCollection ( $prefix )
$prefix string

File collection prefix.

return yii\mongodb\file\Collection

File collection instance.

                protected function selectFileCollection($prefix)
{
    return Yii::createObject([
        'class' => 'yii\mongodb\file\Collection',
        'mongoCollection' => $this->mongoDb->getGridFS($prefix)
    ]);
}

            
tryResultError() protected method

Checks if command execution result ended with an error.

protected void tryResultError ( $result )
$result mixed

Raw command execution result.

throws yii\mongodb\Exception

if an error occurred.

                protected function tryResultError($result)
{
    if (is_array($result)) {
        if (!empty($result['errmsg'])) {
            $errorMessage = $result['errmsg'];
        } elseif (!empty($result['err'])) {
            $errorMessage = $result['err'];
        }
        if (isset($errorMessage)) {
            if (array_key_exists('ok', $result)) {
                $errorCode = (int) $result['ok'];
            } else {
                $errorCode = 0;
            }
            throw new Exception($errorMessage, $errorCode);
        }
    } elseif (!$result) {
        throw new Exception('Unknown error, use "w=1" option to enable error tracking');
    }
}