Class yii\mongodb\Database

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

Database represents the MongoDB database information.

Public Properties

Hide inherited properties

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

Public Methods

Hide inherited methods

Method Description Defined By
clearCollections() Clears internal collection lists. yii\mongodb\Database
createCollection() Creates new collection. yii\mongodb\Database
createCommand() Creates MongoDB command associated with this database. yii\mongodb\Database
dropCollection() Drops specified collection. 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
listCollections() Returns the list of available collections in this database. 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

Property Details

Hide inherited properties

$connection public property

MongoDB connection.

$fileCollection public property

Mongo GridFS collection.

$name public property

Name of this database.

public string $name null

Method Details

Hide inherited methods

clearCollections() public method

Clears internal collection lists.

This method can be used to break cycle references between yii\mongodb\Database and yii\mongodb\Collection instances.

public void clearCollections ( )

                public function clearCollections()
{
    $this->_collections = [];
    $this->_fileCollections = [];
}

            
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 boolean createCollection ( $name, $options = [], $execOptions = [] )
$name string

Name of the collection

$options array

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

$execOptions array

-> goto Command::execute()

return boolean

Whether operation was successful.

throws yii\mongodb\Exception

on failure.

                public function createCollection($name, $options = [], $execOptions = [])
{
    return $this->createCommand()->createCollection($name, $options, $execOptions);
}

            
createCommand() public method (available since version 2.1)

Creates MongoDB command associated with this database.

public yii\mongodb\Command createCommand ( $document = [] )
$document array

Command document contents.

return yii\mongodb\Command

Command instance.

                public function createCommand($document = [])
{
    return $this->connection->createCommand($document, $this->name);
}

            
dropCollection() public method (available since version 2.1)

Drops specified collection.

public boolean dropCollection ( $name, $execOptions = [] )
$name string

Name of the collection

$execOptions array

-> goto Command::execute()

return boolean

Whether operation was successful.

                public function dropCollection($name, $execOptions = [])
{
    return $this->createCommand()->dropCollection($name, $execOptions);
}

            
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];
}

            
listCollections() public method (available since version 2.1.1)

Returns the list of available collections in this database.

public array listCollections ( $condition = [], $options = [], $execOptions = [] )
$condition array

Filter condition.

$options array

Options list.

$execOptions array

-> goto Command::execute()

return array

Collections information.

                public function listCollections($condition = [], $options = [], $execOptions = [])
{
    return $this->createCommand()->listCollections($condition, $options, $execOptions);
}

            
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',
        'database' => $this,
        'name' => $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',
        'database' => $this,
        'prefix' => $prefix,
    ]);
}