Abstract Class yii\mongodb\Migration

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

Migration is the base class for representing a MongoDB migration.

Each child class of Migration represents an individual MongoDB migration which is identified by the child class name.

Within each migration, the up() method should be overridden to contain the logic for "upgrading" the database; while the down() method for the "downgrading" logic.

Migration provides a set of convenient methods for manipulating MongoDB data and schema. For example, the createIndex() method can be used to create a collection index. Compared with the same methods in yii\mongodb\Collection, these methods will display extra information showing the method parameters and execution time, which may be useful when applying migrations.

Public Properties

Hide inherited properties

Property Type Description Defined By
$compact boolean Indicates whether the log output should be compacted. yii\mongodb\Migration
$db yii\mongodb\Connection|array|string The MongoDB connection object or the application component ID of the MongoDB connection that this migration should work with. yii\mongodb\Migration

Public Methods

Hide inherited methods

Method Description Defined By
batchInsert() Inserts several new rows into collection. yii\mongodb\Migration
createCollection() Creates new collection with the specified options. yii\mongodb\Migration
createIndex() Creates an index on the collection and the specified fields. yii\mongodb\Migration
createIndexes() Creates indexes in the collection. yii\mongodb\Migration
dropAllIndexes() Drops all indexes for specified collection. yii\mongodb\Migration
dropCollection() Drops existing collection. yii\mongodb\Migration
dropIndex() Drop indexes for specified column(s). yii\mongodb\Migration
dropIndexes() Drops collection indexes by name. yii\mongodb\Migration
init() Initializes the migration. yii\mongodb\Migration
insert() Inserts new data into collection. yii\mongodb\Migration
remove() Removes data from the collection. yii\mongodb\Migration
save() Update the existing database data, otherwise insert this data yii\mongodb\Migration
update() Updates the rows, which matches given criteria by given data. yii\mongodb\Migration

Protected Methods

Hide inherited methods

Method Description Defined By
beginProfile() Marks the beginning of a code block for profiling. yii\mongodb\Migration
composeCollectionLogName() Composes string representing collection name. yii\mongodb\Migration
endProfile() Marks the end of a code block for profiling. yii\mongodb\Migration
log() Logs the incoming message. yii\mongodb\Migration

Property Details

Hide inherited properties

$compact public property (available since version 2.1.5)

Indicates whether the log output should be compacted. If this is set to true, the individual commands ran within the migration will not be output to the console log. Default is false, in other words the output is fully verbose by default.

public boolean $compact false
$db public property

The MongoDB connection object or the application component ID of the MongoDB connection that this migration should work with. Starting from version 2.0.2, this can also be a configuration array for creating the object.

Method Details

Hide inherited methods

batchInsert() public method

Inserts several new rows into collection.

public array batchInsert ( $collection, $rows, $options = [] )
$collection array|string

Collection name.

$rows array

Array of arrays or objects to be inserted.

$options array

List of options in format: optionName => optionValue.

return array

Inserted data, each row will have "_id" key assigned to it.

                public function batchInsert($collection, $rows, $options = [])
{
    $this->beginProfile($token = "    > insert into " . $this->composeCollectionLogName($collection) . ") ...");
    $rows = $this->db->getCollection($collection)->batchInsert($rows, $options);
    $this->endProfile($token);
    return $rows;
}

            
beginProfile() protected method (available since version 2.1.1)

Marks the beginning of a code block for profiling.

protected void beginProfile ( $token )
$token string

Token for the code block.

                protected function beginProfile($token)
{
    $this->profileTokens[$token] = microtime(true);
    if (!$this->compact) {
        $this->log($token);
    }
}

            
composeCollectionLogName() protected method

Composes string representing collection name.

protected string composeCollectionLogName ( $collection )
$collection array|string

Collection name.

return string

Collection name.

                protected function composeCollectionLogName($collection)
{
    if (is_array($collection)) {
        list($database, $collection) = $collection;
        return $database . '.' . $collection;
    }
    return $collection;
}

            
createCollection() public method

Creates new collection with the specified options.

public void createCollection ( $collection, $options = [] )
$collection string|array

Name of the collection

$options array

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

                public function createCollection($collection, $options = [])
{
    if (is_array($collection)) {
        list($database, $collectionName) = $collection;
    } else {
        $database = null;
        $collectionName = $collection;
    }
    $this->beginProfile($token = "    > create collection " . $this->composeCollectionLogName($collection) . " ...");
    $this->db->getDatabase($database)->createCollection($collectionName, $options);
    $this->endProfile($token);
}

            
createIndex() public method

Creates an index on the collection and the specified fields.

public void createIndex ( $collection, $columns, $options = [] )
$collection string|array

Name of the collection

$columns array|string

Column name or list of column names.

$options array

List of options in format: optionName => optionValue.

                public function createIndex($collection, $columns, $options = [])
{
    $this->beginProfile($token = "    > create index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . empty($options) ? "" : ", " . Json::encode($options) . ") ...");
    $this->db->getCollection($collection)->createIndex($columns, $options);
    $this->endProfile($token);
}

            
createIndexes() public method (available since version 2.1)

Creates indexes in the collection.

public void createIndexes ( $collection, $indexes )
$collection string|array

Name of the collection

$indexes array

Indexes specifications.

                public function createIndexes($collection, $indexes)
{
    $this->beginProfile($token = "    > create indexes on " . $this->composeCollectionLogName($collection) . " (" . Json::encode($indexes) . ") ...");
    $this->db->getCollection($collection)->createIndexes($indexes);
    $this->endProfile($token);
}

            
dropAllIndexes() public method

Drops all indexes for specified collection.

public void dropAllIndexes ( $collection )
$collection string|array

Name of the collection.

                public function dropAllIndexes($collection)
{
    $this->beginProfile($token = "    > drop all indexes on " . $this->composeCollectionLogName($collection) . ") ...");
    $this->db->getCollection($collection)->dropAllIndexes();
    $this->endProfile($token);
}

            
dropCollection() public method

Drops existing collection.

public void dropCollection ( $collection )
$collection string|array

Name of the collection

                public function dropCollection($collection)
{
    $this->beginProfile($token = "    > drop collection " . $this->composeCollectionLogName($collection) . " ...");
    $this->db->getCollection($collection)->drop();
    $this->endProfile($token);
}

            
dropIndex() public method

Drop indexes for specified column(s).

public void dropIndex ( $collection, $columns )
$collection string|array

Name of the collection

$columns string|array

Column name or list of column names.

                public function dropIndex($collection, $columns)
{
    $this->beginProfile($token = "    > drop index on " . $this->composeCollectionLogName($collection) . " (" . Json::encode((array) $columns) . ") ...");
    $this->db->getCollection($collection)->dropIndex($columns);
    $this->endProfile($token);
}

            
dropIndexes() public method (available since version 2.1)

Drops collection indexes by name.

public void dropIndexes ( $collection, $indexes )
$collection string|array

Name of the collection

$indexes string

Wildcard for name of the indexes to be dropped.

                public function dropIndexes($collection, $indexes)
{
    $this->beginProfile($token = "    > drop indexes '{$indexes}' on " . $this->composeCollectionLogName($collection) . ") ...");
    $this->db->getCollection($collection)->dropIndexes($indexes);
    $this->endProfile($token);
}

            
endProfile() protected method (available since version 2.1.1)

Marks the end of a code block for profiling.

protected void endProfile ( $token )
$token string

Token for the code block.

                protected function endProfile($token)
{
    if (isset($this->profileTokens[$token])) {
        $time = microtime(true) - $this->profileTokens[$token];
        unset($this->profileTokens[$token]);
    } else {
        $time = 0;
    }
    if (!$this->compact) {
        $this->log(" done (time: " . sprintf('%.3f', $time) . "s)\n");
    }
}

            
init() public method

Initializes the migration.

This method will set $db to be the 'db' application component, if it is null.

public void init ( )

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::className());
}

            
insert() public method

Inserts new data into collection.

public \MongoDB\BSON\ObjectID insert ( $collection, $data, $options = [] )
$collection array|string

Collection name.

$data array|object

Data to be inserted.

$options array

List of options in format: optionName => optionValue.

return \MongoDB\BSON\ObjectID

New record id instance.

                public function insert($collection, $data, $options = [])
{
    $this->beginProfile($token = "    > insert into " . $this->composeCollectionLogName($collection) . ") ...");
    $id = $this->db->getCollection($collection)->insert($data, $options);
    $this->endProfile($token);
    return $id;
}

            
log() protected method (available since version 2.1.1)

Logs the incoming message.

By default this method sends message to 'stdout'.

protected void log ( $string )
$string string

Message to be logged.

                protected function log($string)
{
    echo $string;
}

            
remove() public method

Removes data from the collection.

public integer|boolean remove ( $collection, $condition = [], $options = [] )
$collection array|string

Collection name.

$condition array

Description of records to remove.

$options array

List of options in format: optionName => optionValue.

return integer|boolean

Number of updated documents or whether operation was successful.

                public function remove($collection, $condition = [], $options = [])
{
    $this->beginProfile($token = "    > remove " . $this->composeCollectionLogName($collection) . ") ...");
    $result = $this->db->getCollection($collection)->remove($condition, $options);
    $this->endProfile($token);
    return $result;
}

            
save() public method

Update the existing database data, otherwise insert this data

public \MongoDB\BSON\ObjectID save ( $collection, $data, $options = [] )
$collection array|string

Collection name.

$data array|object

Data to be updated/inserted.

$options array

List of options in format: optionName => optionValue.

return \MongoDB\BSON\ObjectID

Updated/new record id instance.

                public function save($collection, $data, $options = [])
{
    $this->beginProfile($token = "    > save " . $this->composeCollectionLogName($collection) . ") ...");
    $id = $this->db->getCollection($collection)->save($data, $options);
    $this->endProfile($token);
    return $id;
}

            
update() public method

Updates the rows, which matches given criteria by given data.

Note: for "multiple" mode Mongo requires explicit strategy "$set" or "$inc" to be specified for the "newData". If no strategy is passed "$set" will be used.

public integer|boolean update ( $collection, $condition, $newData, $options = [] )
$collection array|string

Collection name.

$condition array

Description of the objects to update.

$newData array

The object with which to update the matching records.

$options array

List of options in format: optionName => optionValue.

return integer|boolean

Number of updated documents or whether operation was successful.

                public function update($collection, $condition, $newData, $options = [])
{
    $this->beginProfile($token = "    > update " . $this->composeCollectionLogName($collection) . ") ...");
    $result = $this->db->getCollection($collection)->update($condition, $newData, $options);
    $this->endProfile($token);
    return $result;
}